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?

nbrooks
  • 18,126
  • 5
  • 54
  • 66
FaisalHBTI
  • 116
  • 6
  • there is lot more then primitive datatypes in Java. that is way we have default constructor. – Vishrant Feb 25 '16 at 06:07
  • that thing is not resolved by default constructor created by compiler.For that we manually define a default constructor. .....Vishrant – FaisalHBTI Feb 25 '16 at 06:31

3 Answers3

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.

Community
  • 1
  • 1
Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • that is related to super, friend. I wanna say what is the need of initialization, if by default java has default values – FaisalHBTI Feb 25 '16 at 08:20
  • 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. – Vishrant Feb 25 '16 at 17:01
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

Khan Abdulrehman
  • 816
  • 2
  • 10
  • 22
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
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123