Parameterized Constructor, constructor present in same class, with method example 2

Java Programming Language / Class, Object and Methods in java

931

Program:

 class ExampleClass
{
      private int var;
      // this is constructor
      public ExampleClass(int num)
      {
             var=num;
      }
      // this is method
      public int getValue()
      {
              return var;
      }
      public static void main(String args[])
      {
              ExampleClass myobj = new ExampleClass(2);
              System.out.println("value of var is: "+myobj.getValue());
      }
}

Output:

value of var is: 2
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.