5.4 Constructors & InstantiationHomepage « Java6 Certification « 5.4 Constructors & Instantiation
In this lesson we look at code that declares and/or invokes overridden or overloaded methods and code that declares and/or invokes superclass or overloaded constructors.
Lets take a look at the points outlined at the Oracle Website for this part of the certification.
- Section 5: OO Concepts
- Given a scenario, develop code that declares and/or invokes overridden or overloaded methods and code that declares and/or invokes superclass or overloaded constructors.
Constructors
Top
Constructors allow us to instantiate our objects via declaration, assignment and creation.

- Declaration - Here. we declare a reference variable named
moggy
of typeCat
and the JVM allocates space for it. - Creation - Tells the JVM to allocate space on The Heap for a new
Cat
object. - Assignment - Assign the new
Cat
object to the reference variablemoggy
.
Access Modifiers
The table below shows the types of access available in Java for constructors.
Access modifier | Description |
---|---|
public | A constructor may be declared with the public access modifier, and if it is the constructor is accessible to all other classes everywhere, assuming the class it resides in is accessible. |
protected | A constructor may be declared with the protected access modifier, and if so, it is only accessible to the package
the implementation is in.See the Packages lesson for more information on packaging. |
no modifier package-private / (the default) | If a constructor has no explicit access modifier, it is only accessible to the package the implementation is in.
See the Packages lesson for more information on packaging. |
private | A constructor may be declared with the private access modifier, and if it is the constructor can only be constructed from within its own class. |
Constructor Checklist
- A constructor runs when we code the
new()
operator followed by a class name. - Constructors must have the same name as the class and no return type.
- Constructors are used to initialize the instance variables (object state) of the object instance being constructed.
- If you don't code a constructor in your class the compiler will put in a default no arguments constructor.
- If you do code a constructor in your class, the compiler will NOT put in a default no arguments constructor, you will have to code it yourself.
- We can have more than one constructor in a class and the constructors are then known as overloaded constructors.
- When using overloaded constructors in a class, each constructor must have different argument lists so the compiler knows which constructor to use to construct our objects. Having the same argument types is fine as long as the order differs.
- You can refer to any member of the current object from within a non-static method or constructor by using the
this()
keyword. - You can invoke one constructor from another constructor within the same class by calling
this()
and doing so is known as explicit constructor invocation and is the only way to invoke a constructor. - If we decide to use
this()
it must be the first statement within our constructor or we get a compiler error. This means we can't usethis()
andsuper()
together. - We can use
super()
to invoke a superclass constructor and if we don't supply this explicitly, then the compiler inserts a no-argssuper()
for us as the first statement in the constructor if we haven't used thethis()
keyword. - When explicitly coding
super()
we can supply arguments to invoke a constructor in the superclass matching the signature of our call. - When explicitly coding
super()
it must be the first statement within the constructor or we get a compiler error. This means we can't usesuper()
andthis()
together. - When explicitly coding
super()
only methods and static variables can be used within the call. - Interfaces do not have constructors as they are not part of a particular classes inheritance tree.
- Abstract classes do have constructors, although we can't code them, as they are part of a classes inheritance tree and so are called via
super()
on concrete subclass instantiation.
/*
Some code showing constructor usage
*/
public class A {
public static void main(String args[]) {
A a = new A(); // OK, compiler puts in a default no arguments constructor so we can instantiate
}
}
public class B {
B() { } // OK, we code our own default no arguments constructor
}
public class C {
void C() { } // OK, this is a method with the same name as the class as it has a return type
public static void main(String args[]) {
C c = new C();
}
}
public class D {
D() { }
D(int i) { } // OK, overloaded constructor
public static void main(String args[]) {
D d = new D();
D d2 = new D(5);
}
}
public class E {
int i;
String s;
E(int i) {
this(i, "unknown"); // Invoke one constructor from another constructor within the same class
}
E(int i, String s) {
this.i = i;
this.s = s;
}
public static void main(String args[]) {
E e = new E(5);
}
}
/*
Following code will fail as compiler inserts a no arguments constructor for us which invokes
super() and there isn't a no arguments constructor in superclass
*/
public class F extends E {
}
public class G extends E {
G(int i) { // here we invoke existing super constructor so works fine
super(i);
}
public static void main(String args[]) {
G g = new G(5);
}
}
Related Java6 Tutorials
Objects & Classes - Reference Variables - The new Operator
Objects & Classes - Constructors
Inheritance Concepts -Superclass Constructors