Site icon DataFlair

Java Constructor with Examples

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

New! Keyboard shortcuts … Drive keyboard shortcuts have been updated to give you first-letters navigation
class Abc
{
      int a,b;

    Abc(int a,int b)
    {
          this.a=a;
          this.b=b;
    }
    Abc(int a)
    {
          this.a=a;
          this.b=100;
    }
    Abc()
    {
        this.a=400;
        this.b=300;
    }

    void add()
    {
        System.out.println(a+b);
    }
}

class TestConstructor
{
      public static void main(String args[])
      {
                   Abc A1=new Abc(50,20);  
                   Abc B1=new Abc(500,200);  
                   Abc C=new Abc();
                  A1.add();
                  B1.add();

      }
}

 

Exit mobile version