简体   繁体   中英

what is the use of a parameterized constructor in java?

Im a beginner in java and i wanted to know in a simple way why you should use a parameterized constructor instead of a default one provided by the compiler.

Thanks

You'd need to use parameterized objects anytime you want to pass any sort of configuration into your object. You could use setters to pass in that information later, but not only is it shorter and cleaner to pass that information in at construction time, it lines up with the good practice of creating immutable objects, which cannot be modified after construction and have no setters. For example,

class Student {
  private final String name;
  private final Date birthday;
  public Student(String name, Date birthday) {
    this.name = name;
    this.birthday = birthday;
  }
}  

In Java, a constructor is a method which is called by Java runtime during the object creation using the new operator. A reason for creating a constructor could be:

  • To initialize your object with default or initial state since default values may not be what you are looking for. For example, if you have a person class, containing a name and date of birth, you want these fields to be filled, not empty. So you would pass the values of the name and date of birth into the constructor of that class to assign the object with a value to use in that class.

As in any object oriented language, a constructor method is used to allocate and initialize the memory for an object. With this in mind, a parameterized constructor method is used for setting properties of the object to certain value, while the default won't set any value to any of the properties.

In addition to what Louis Wasserman said, there are many situations where paramaterizing constructors makes sense from a logical point of view. Say you want to make an Employee object. There are certain attributes an employee MUST have to even be considered an employee, like a name, social security number, company and ofcourse a salary (slavery is illegal). Therefore, it would be logical to make a parameterized constructor requiring the 4 aforementioned variables.

By the default constructor any attributes your object might have are set to 0, false et cetera. If you want to set the attributes right away you can use a parameterized constructor. Also using you own constructor of course gives you the option of executing code before the object (technically while) is created.

By the way: The answer that "the default won't set any value to the properties" is wrong. For example this code:

public class Test {
    private int test;
    private boolean test2;

    public static void main(String[] args) {
        Test test = new Test();
        System.out.println(test.test);
        System.out.println(test.test2);
    }
}

Will print out "0" and "false".

Constructor helps class to initialize and set values to there some properties that are passed to it.That may be for current object of class. For Ex.

class Education{
        String typeOfEducation;     //values are not set 
        String  courseName;          
Education(String type,String name){
typeOfEducation=type;
courseName=name;                    //values are set   
                }
                   }

Hope this helps.

I'm surprised no one has brought up overloading in this thread. Parameterized constructors also give the user the ability to set up an object to varying degrees based on information you have at creation time. You can do this with method overloading.

For example:

public Car(long vin){
    this.vin = vin;
}

would set up a car object with only a vin number whereas this:

public Car(long vin, String make){
    this.vin = vin;
    this.make = make;
}

would create a similar Car object with additional information. In addition to overloading, you ought to look into copy constructors as well to get an idea of 'shallow' vs. 'deep' object copies. The following link has a decent example: http://www.geeksforgeeks.org/copy-constructor-in-java/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM