Tamil Maran
Tamil Maran

Reputation: 291

Why do we need default constructor when we can initialize data members in java?

My question is, when we declare a java class with data members we normally initialize them using constructors(With an exception to static members). But the data members can also be initialized directly without calling the constructor.

public class Box {
      public int height = 0;
      public int width = 0;
}

When this is possible, why do we need a default constructor? like this

public Box(){
      height = 0;
      width = 0;
}

The memory for an object is created when it is instantiated. But if memory for data members are allocated while creating objects then how is it possible to initialize data members at the beginning of the class?

Upvotes: 4

Views: 2562

Answers (4)

Arkantos
Arkantos

Reputation: 6608

Constructor is meant to initialize an object, to add some state, to make the object ready for use before we start calling methods on it. Initializing an object can be anything like setting some values for instance variables other than the default values, or adding items to an ArrayList or Map to use those values in the methods defined in that class.

For better understanding, I recommend you to refer to this link - Class and Object Initialization

Upvotes: 0

nos
nos

Reputation: 229088

For a simple case where you can initialize the data members, as in your example, you don't need a default constructor.

However you might want to do more things in a default constructor than initialize some fields;

Call a non-default constructor of the super class:

public class Box extends Shape {
      public int height = 0;
      public int width = 0;

      public Box() {
         super("Box");
}

Do some work as part of the constructor :

public class Boxes {
     private HashMap<String, Box> boxes = new HashMap<String, Box>();

      public Boxes() {
         boxes.put("2x2", new Box(2,2));
         boxes.put("4x4", new Box(4,4));
         ....
}

Provide a non-default constructor as well as a default constructor.

If you create a non-default constructor, there will not be a default constructor created automatically.

public class Box {
      public int height;
      public int width;

      public Box() {
         height = 0;
         width = 0;
         //or simply call Box(0,0)
      }
      public Box(int height, int width) {
         this.height = heigth;
         this.width = width;
      }
}

without code for the no-argument Box() constructor, you can't do new Box() since there is a Box(int height, int width) constructor. However if you don't write any constructors, the compiler creates the no-argument Box() constructor for you.

Upvotes: 3

kamoor
kamoor

Reputation: 2939

You will need constructor to execute more complex operations than just initializing with a constant value. For example you want to dynamically pass an object in to another class while creating instance. A simple documentation can be found here

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

You don't specifically need a default constructor if your class doesn't have other constructors and an external framework (or probably your own code) could create instances of it and needs to use the default constructor.

Some examples:

  • Jackson for JSON deserialization using ObjectMapper#readValue
  • JSF when creating managed beans

If you don't have this scenario, then you don't even need to add a default constructor, the compiler will do it for you.

Upvotes: 0

Related Questions