How do I write constructor names in Java?

Java 8Object Oriented ProgrammingProgramming

Declaring a constructor is in Java is similar to methods. While naming the constructor of a class in Java you need to keep the following points in mind.

  • The name of the constructor should be the same (same case) as the class name (along with the access specifier).
  • A constructor should not have any return type.
  • Constructor cannot be static, final, abstract and, synchronized.

Example

Following Java program is an example of a constructor.

Live Demo

public class Sample {
   Sample() {
      System.out.println("This is an example of a constructor");	   
   }
   public static void main(String args[]) {
      new Sample();
   }
}

Output

This is an example of a constructor
raja
Published on 05-Jan-2018 10:07:32
Advertisements