Object Creation in Java
Student bob = new Student();
This is how we usually create an object in Java.
As Java doc says, we can break this statement into three parts:
- Declaration
- Instantiation
- Initialization
1. Declaring a variable to refer an object
Student bob;
In here we declare reference variable bob
of type Student
.
Declaring a reference variable does not create object. At this state bob
references to nothing. It’s value will be undetermined until we create an object and assign to it.
2. Instantiating a class
The new
keyword is a Java operator that:
- Instantiates a class by allocating memory for a new object on the heap at runtime.
- Returns a reference to that memory.
- Invokes the object constructor.
We have to provide a postfix argument to the new
operator. The argument is the constructor name whose class we are going to instantiate.
Student bob = new Student();
In here we are going to instantiate or create an object of Student class.
The new
operator returns a reference to the object it created. This reference is stored to a variable of the appropriate type. In our example bob
will store the newly created object’s reference.
NOTE: Instantiating a class means the same thing as creating an object. When you create an object, you are creating an instance of a class.
3. Initiating an object
Constructors initialize the instance variables associated with its class. So that constructors can also be called as initializers. Instance variables will be initialized to their default values if not explicitly defined by the programmer.
Constructors initialize those variables from inside out (The constructors are executed from all the way up the inheritance hierarchy to down).
It goes all the way up to the Object class and comes back down. While coming down, it initializes all those variables.
In our example, constructor will initialize not only Student class instance variables but also its super class instance variables (in our example we did not define super class but compiler will put Object class as a super class when our class do not have one.
The reason for the inside out is, the very first line of code in constructor is either this()
or super()
.
this()
: refers to current class constructor
super()
: refers to super class constructor
Even if don’t put this()
or super()
at the vert first line of constructor code, the compiler will put super()
for us.
class A {
public A() {
//super(); // hidden line, added by compiler
System.out.println("Constructor A");
}
}
class B extends A{
public B(){
//super(); // hidden line, added by compiler
System.out.println("Constructor B");
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
}
}
Output:
Constructor A
Constructor B
In above example, compiler put super()
for us.
NOTE: If a constructor does not explicitly invoke a super class constructor, the Java compiler automatically inserts a call to the no-argument constructor of the super class. If the super class does not have a no-argument constructor, you will get a compile-time error. Object class does have such a constructor, so if Object is the only super class, there is no problem.
If you explicitly create a constructor, let it be no-arg or parameterized, the compiler will not create default constructor for you.
NOTE: default constructor is a no-arg constructor that’s created by compiler. If you create such no-arg constructor it should not be referred to as default constructor but as no-arg constructor.
class A {
public A(int a) {
System.out.println("Constructor A with parameters");
}
}
class B extends A{
public B(){
System.out.println("Constructor B");
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
}
}
It will cause compile-time error. We explicitly created parameterized constructor for class A, so compiler will not create default constructor for us. We forgot to add it inside the class B constructor at the very first line. So the compiler will call to no-arg super class constructor which is not there. So it will cause error.
This one is okay:
class A {
public A() {
System.out.println("Constructor A");
}
public A(int a) {
System.out.println("Constructor A with " + a);
}
}
class B extends A{
public B(){
super(5);
System.out.println("Constructor B");
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
}
}
/*
Output: Constructor A with 5
Constructor B
*/
We’re calling parameterized super class constructor in class B, so this one is okay.
Now, we know how objects are created in Java and their inner mechanisms.
I would like to recommend you to read these documents and articles to get deep understanding about this topic:
- Creating Objects: Java documentation
- new operator in Java
- 9 Rules about Constructors in Java
- Difference between super() and this() in java
Thank you for reading.
Happy Coding!