How do I write constructor names in Java?


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

Updated on: 30-Jul-2019

903 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements