Java – Constructors

Home/Java - Tutorial/Java - Class/Java – Constructors
Java – Constructors 2017-08-09T10:12:53+00:00

Java supports a special type of methods, called constructor that enables an object to initialize itself when it is created. Java Constructors have the same name as the class it-self. Java Constructors do not specify a return type, not even void. This is because they return the instance of the class itself. A constructor is automatically called when an object is created.

Syntax:

Constructor_name([arguments])

{

// body

}

Constructors are generally of two types.

1.      Non-Parameterized

2.      Parameterized  

1.      Non-Parameterized:


 


Output:
—–Printing the coordinates—–
                   10   20
2.      Parameterized:

?

 


Output:
Enter int value for i : 10
Enter int value for k : 20
—–Printing the coordinates—–
                   10   20

Prev Next