Wednesday, 9 April 2014

Constructor in Java

Constructor in Java

Constructor java tutorial: Java constructors are the methods which are used to initialize objects. Constructor method has the same name as that of class, they are called or invoked when an object of class is created and can't be called explicitly. Attributes of an object may be available when creating objects if no attribute is available then default constructor is called, also some of the attributes may be known initially. It is optional to write constructor method in a class but due to their utility they are used.

Java constructor example

class Programming {
  //constructor method
  Programming() {
    System.out.println("Constructor method called.");
  }
 
  public static void main(String[] args) {
    Programming object = new Programming(); //creating object
  }
}

No comments:

Comment

Comment Box is loading comments...