Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best explain and use empty Constructors in Java? [duplicate]

I have been self-learning Java. I understand the scope of defining a class, but still didn't get the concept of an empty constructor usage.
Usually we should pass parameters for constructor to build instance object. But, I often see empty parameter for constructor. For example:

 class Person {
   String name;
       int age; 

 public Person();

 public Person(String name, int age){
     this.name = name;
     this.age = age;
     } 
 }

I researched and read an example that using a class "fish" to explain. So, this is what I understood so far: when defining a class, we first define properties for an object, then we create a constructor that will build the object with methods. Empty constructor build GENERIC object, and constructor with parameters build objects with more specific information. Let’s say the example above, if I create an instance object using the empty constructor:

Person p1 = new Person();  

-- it will still create an object but without any properties in it? So, what exactly the empty constructor is used for? I saw it in a lot of example codes. Is it very useful/common?

Thanks for looking and answering!

like image 398
Sophia Ngo Avatar asked Sep 24 '13 23:09

Sophia Ngo


4 Answers

If you write a constructor with parameters, then you need to declare the default one (if you want to use it)

class Person {
    String name;
    int age; 

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    } 
 } 

You can't do this now:

Person p = new Person();

In order to use the "default" constructor (with no parameters) you will need to declare it:

class Person {
    String name;
    int age; 

    public Person(){
        name = "Man With No Name";  //sometimes you will want to set the variables 
        age = 21;     //to some default values
    }

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    } 
 } 
like image 196
jsedano Avatar answered Nov 16 '22 21:11

jsedano


A lot of software written in java these days takes advantage of frameworks that operate directly on your code at runtime by using special JVM features (known as reflection) to just access your class directly and mess around with it, rather than access it through the code you actually wrote.

One common example of such a library is Hibernate. It "magically" takes information from a relational database and assigns the database values to the fields of an object. Your class is enhanced with additional code added at run time to make it magically map back to the contents of the database.

It is a consequence of the reflection API that it's a real pain in the butt to work with classes that do not have a 'default' constructor. You would have to supply a lot of additional information about how to correctly use your class's constructors in a way that can be programmatically interpreted.

Instead, the tools just require that classes have an empty, default, constructor, to make it easy to enhance the class at runtime.

Hence, we tend to leave an empty constructor always, even when creating an object without supplying certain values doesn't really make sense according to our program's API.

like image 25
Affe Avatar answered Oct 10 '22 12:10

Affe


There are three common reasons to define a default constructor:

  • To construct an object with default values.
  • To initialize an object that doesn't need parameters in that initialization process.
  • To redefine the scope of the constructor. Making the constructor private will prevent anyone but the class itself from constructing an object.
like image 11
tbodt Avatar answered Nov 16 '22 20:11

tbodt


I am sure other answers would come but in short -

Empty constructor just gives you an instance of that object. You might use setters on it to set necessary properties. If you want to make sure that any instance created is always valid and any member variables are always initialized,then you would define the constructor which initializes all the required member variables.

Also if you use frameworks like Spring, default constructor is often used where properties are set using setter injection.

like image 3
RandomQuestion Avatar answered Nov 16 '22 19:11

RandomQuestion