Example of parameterized constructor?

In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.


								
package com.mindclues;

class ParameterizedCunstrutor {

    int id;
    String name;

    ParameterizedCunstrutor(int i, String n) {
        id = i;
        name = n;
    }

    void display() {
        System.out.println(id + " " + name);
    }

    public static void main(String args[]) {
        ParameterizedCunstrutor s1 = new ParameterizedCunstrutor(1, "Amit");
        ParameterizedCunstrutor s2 = new ParameterizedCunstrutor(2, "Rajeev");
        s1.display();
        s2.display();
    }
}

							

								
1 Amit
2 Rajeev
							

Related Articles

post a comment