Pages

Parameterized constructor in java

What is a Parameterized constructor?

Parameterized Constructor:

Constructor having arguments called Parameterized constructor.
Syntax:
     className(arg1,arg2,...){
      //block of code
     }
Demo on Parameterized Constructor:
public class ParameterConstructor {
    String Bank;
    String Name;
    int Mobile;

    ParameterConstructor() { //default constructor
        System.out.println("Default constructor");
    }

    ParameterConstructor(String bk, String nm, int mob) {
        Bank = bk;
        Name = nm;
        Mobile = mob;
    }

    public static void main(String args[]) {
        ParameterConstructor pc = new ParameterConstructor();
        ParameterConstructor pc1 = new ParameterConstructor("SBI", "personName", 123456789);
        System.out.println(pc1.Bank);
        System.out.println(pc1.Name);
        System.out.println(pc1.Mobile);
        ParameterConstructor pc2 = new ParameterConstructor("Axis", "personName", 123456789);
        System.out.println(pc2.Bank);
        System.out.println(pc2.Name);
        System.out.println(pc2.Mobile);
    }
}
Output:
Parameterized constructor in java
Parameterized constructor in java

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.