3

We know the default value of primitive data types is provided by java e.g: for int we have 0. We have a default constructor in java which also does the same job. What's the need for that? The state of any object would be the same by default if java did not have the default constructor.

I am not asking for an answer with respect to beans but for the sole purpose i.e. initialization. Why do we have a default constructor?

2
  • there is lot more then primitive datatypes in Java. that is way we have default constructor. Commented Feb 25, 2016 at 6:07
  • that thing is not resolved by default constructor created by compiler.For that we manually define a default constructor. .....Vishrant Commented Feb 25, 2016 at 6:31

3 Answers 3

2

Its not only about only primitive datatype initialization but the initialization of class member datatype.

For example: when you are creating object of a class and not defining default constructor and any parameterised constructor , then JVM will add default constructor which will have call to constructor of its base class just to make sure all the base class member variables are initialized.

Also please check this answer for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

that is related to super, friend. I wanna say what is the need of initialization, if by default java has default values
just think about how and when initialization will be done, at least there has to be some place where these default values (even though it is primitive type) must be assigned, so if you will start debugging you will find when call is reaching to constructor it will first initialize class level variable then the cursor will move to next line of constructor.
1

Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

clearly says constructor provides data for the object.

class Student{  
int id;  
String name;  

void display(){System.out.println(id+" "+name);}  

public static void main(String args[]){  
Student s1=new Student();  
s1.display();  
}  
}  

output is 0 null

class Student{  
int id;  
String name;  

Student()
{
  id=1;
  name="abc";
}

void display(){System.out.println(id+" "+name);}  

public static void main(String args[]){  
Student s1=new Student();  
s1.display();  
}  
}  

in this case output will be 1 abc

It's not mandatory to define default constructor, but if you are writing Hibernate persistent class, JPA entities or using Spring framework to manage object creation and wiring dependencies, you need to be bit careful. Many of open source framework, uses reflection to create instance or Object at runtime, based upon name of class. For example When Hibernate creates instance of entities using reflection it uses Class.newInstance() method, which require a no argument constructor to create an instance. It's effectively equivalent of new Entity(). This method throws InstantiationException if it doesn't found any no argument constructor in Entity class, and that's why it's advised to provide a no argument constructor.

Why Default or No Argument Constructor is Important in Java Class

Comments

0

The main purpose of the default constructor is calling the base class's constructor. Given the following class:

public class Default {

    private int result;
    private Object result1;

    public int getResult() {  return result; }
    public Object getResult1() {  return result1; }
}

results in the following disassembled byte code:

> javap -c Default.class
Compiled from "Default.java"
public Default {
  public Default();
    Code:
       0: aload_0
       1: invokespecial #12                 // Method java/lang/Object."<init>":()V
       4: return

  public int getResult();
    Code:
       0: aload_0
       1: getfield      #20                 // Field result:I
       4: ireturn

  public java.lang.Object getResult1();
    Code:
       0: aload_0
       1: getfield      #24                 // Field result1:Ljava/lang/Object;
       4: areturn
}

The only purpose of the default constructor in this case is to call super() to execute the super class's default (or no-arg) constructor. Still, result is initialized to 0 and result1 is initialized to null - this is done by the runtime environment when the instance is created.

If any of the members are explicitly initialized, this is also done in the default constructor:

...
private int result = 42;
...
  public Default();
    Code:
       0: aload_0
       1: invokespecial #12                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: bipush        42
       7: putfield      #14                 // Field result:I
      10: return

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.