A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Constructors:

Constructors in Java

What are the Constructors in Java?


When discussing classes one of the most important sub-topics would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.

Each time a new object is created at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.

Example of a constructor in java is given below::-






public class Puppy
{
 public puppy()
 {
   //This is default constructor
 }
 
 public puppy(String name)
 {
  //This constructor has one parameter, name.
 }
}


 


Java also supports Singleton Classes where you would be able to create only one instance of a class.

LEARN TUTORIALS

.

.