Java-Constructor with Parameter

Constructor with Parameter

A constructor is have parameters is known as parameterized constructor. A constructor can take zero, one, or more parameters.

Program

31

Program Source

class Data{
    
    int data;
    
    Data(int d)
    {
        data = d;
    }
    
    void printData()
    {
        System.out.println("data : "+data);
    }
}

public class Javaapp {

    public static void main(String[] args) {
       
        Data d1  = new Data(50);
        d1.printData();
    }
}

Leave a Comment