简体   繁体   中英

When to use Parameterized Method vs Parameterized Constructor in Java

I have just started learning Java and came to this topic of Parameterized method and Parameterized constructor from Java Ninth Edition Herber Schildt[chapter 6] ,

I understood how to declare them and how do they work, but I am confused about their usages, when to use Parameterized Method and when to use parameterized constructor?

Please give me an example with simple analogy.

They have a main difference which is the return type. A constructor doesnt return anything , not even void. A constructor is run when a class is instantiated. On the other hand, parameterized methods are used for type safety, ie to allow different classes without casting.

 public class Person {

        String name ;
        int id;

//Default constructor
        Person(){

        }

//Parameterized constructor
        Person(String name, int id){
            this.name = name;
            this.id = id;

        }

        public static void main(String[] args) {
            Person person = new Person("Jon Snow",1000); // class instantiation which set the instance variables name and id via the parameterised constructor

            System.out.println(person.name); //Jon Snow
            System.out.println(person.id); //1000

            Person person1 = new Person(); //Calls the default constructor and instance variables are not set

            System.out.println(person1.name); //null
            System.out.println(person1.id); //0
        }

    }

First of all you should understand what the constructor actually means.

You can consider "Constructor" as a function that gets called whenever you create an object for a class. Inside a class you can have many instance variables ie Every object of that class will have its own copy of its instance variables. Thus whenever you create a new object using the new ClassName (args) statement, you can initialize the values of the instance variables of that object within the constructor since it will be called first whenever you instantiate an object. Remember constructor would be called just once when the object is created.

Moreover there are 2 types of methods instance and static methods. Both types can accept parameters. If you are using static parametrized method, then you cannot call the method on the object of the class, since they can operate on static variables of the class.

Instance parametrized methods can use both instance and static members of the class and they are called on an object. They represent a machinery to do certain operation on the Object on which you are calling the method upon. The Parameters passed inside these methods ( both static and instance) can act as the input to the operation which you want to accomplish. Unlike constructor which was called only once upon instatantion of the object, You can call the methods as many times as you want.

Another classic difference is that the constructor always returns the reference to the object. This is the default implicit behavior and we do not need to specify this explicitly in the signature. However method can return anything as per your choice.

Example:

public class A {

    String mA;

     /* Constructor initilaizes the instance members */
     public A (String mA) { 
         this.mA = mA;
     } 

     /* this is parameterized method that takes mB as input
        and helps you operate on the object that has the instance
        member mA */

     public String doOperation (String mB) {
         return mA + " " + mB;
     }

     public static void main(String args[]) {

          /* this would call the constructor and initialize the instance variable with "Hello" */
         A a = new A("Hello");

        /* you can call the methods as many times as            you want */
        String b=  a.doOperation ("World");
        String c = a.doOperation ("How are you");

     }
}

Method & Constructor are totally different concept & serves different purpose.

Constructor is specified for initializing your Object and any setting/data set when object creates.You can't call the constructor again & again . It will be called once per Object(You may call one constructor from another constructor of that class)

Method are a functional unit , you can call/reuse them as much as you want.

Hope It was helpful Thanks,

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