Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the extra benefit of creating constructor in java?

I have noticed a thing that a constructor and a simple method of a class do the same work. what is the exact reason to create a construct of a class? If i create MyClass(){} constructor and MyClassMethod(){} method it will do the same work as I write the body part of those method and constructor. So what is the need of construct? Does it have any special use ?

like image 675
Tushar Monirul Avatar asked Jul 18 '13 10:07

Tushar Monirul


3 Answers

A constructor and a method are two different things. The fact that you can write the same or similar code inside them is irrelevant.

When a new object is created a constructor is called. If you don't specify one the compiler will create a default one for you. This is the place where initializaton of the object's fields takes place and memory is allocated for the object. This is a concept that all object-oriented languages have. A new object must be initialized somehow. Memory needs to be allocated. In Java you don't manage the memory yourself (at least not directly anyway) so this is the purpose of the constructor. Note that since a constructor is always executed, this behaviour is enforced as soon as you call e.g. Person p = new Person();.

Now since a constructor is always being called, you have an option here: do you let the default constructor execute or do you create one yourself? Perhaps there are fields that need to be initialized in a way other than their default values. Or perhaps you need to not allow creating an object without providing some values. If you define a constructor yourself, the compiler does not create a default one for you. So if I have public Person(String firstName, String lastName) {} then I have created a specific rule that is again enforced by the system: a new object of class Person cannot be created unless you give a first name and last name:

Person p = new Person(); // this would give a compile error
Person p = new Person("John", "Smith"); // this is the only way to create an object now

Using a method you cannot enforce this. The programmer using your class might call your method or not. The constructor is a part of the lifecycle of the object. Methods define the behaviour of the object

like image 127
c.s. Avatar answered Nov 02 '22 10:11

c.s.


Some points :

1) Constructors are the only way to set final instance variables .

public class SomeType {
   final int x ;
   SomeType(int y){
     x=y;
   }
}

2) A class with private constructor cannot be sub classed.

3) If your class is a subclass and the base class doesn't have a default constructor , then you need a constructor in your class to call the super class constructor.

like image 5
AllTooSir Avatar answered Nov 02 '22 09:11

AllTooSir


One of the benefits of using a constructor over a method is that you can be assured the constructor was called and the work within the constructor was performed.

The language specifies that to construct an object a constructor must be called. So if you use a custom method to establish the initial state of your object, you will need to call the default constructor first. Why make two method calls when you can perform the work in one call the constructor and be assured the object has been properly initialized?

public class Test(){

    private Integer x;

    public Test(){

    }

    public Test(Integer x){
       this.x = x;
    }

    public void setX(Integer x){
       this.x = x;
    }

    public void doSomethingWithX(){
       this.x.toString();
    }
}

Test test = new Test(8);
test.doSomethingWithX(); //I know x has been declared and assigned

Test test = new Test();
test.doSomethingWithX(); //X has not been assigned results in NPE
like image 4
Kevin Bowersox Avatar answered Nov 02 '22 10:11

Kevin Bowersox