In this tutorial, we are going to write a Java program using a constructor in Java Programming with practical program code and step-by-step full complete explanation.

Java program using constructor


Java program using the constructor.

 
class A
{
    int a;
    
    public A(int x)
    {
        a = x;
    }

    public A()
    {
        System.out.println("it is default constructor");
    }
    {
        System.out.println("it is funny");
    }

    public void display()
    {
        System.out.println("a="+a);
    }

    public static void main(String arg[])
    {
        A x = new A();
        x.display();
        A y = new A(10);
        y.display();
    }
}


Output

 
it is funny
it is default constructor
a = 0
it is funny
a = 0