
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Why the constructor name is same as the class name in Java?
Every class object is created using the same new keyword, so it must have information about the class to which it must create an object. For this reason, the constructor name should be the same as the class name.
Example
class MyConstructor{ public MyConstructor() { System.out.println("The constructor name should be same as the class name"); } public static void main(String args[]){ MyConstructor mc = new MyConstructor(); } }
In the above program, the constructor name should be the same as the class name (MyConstructor).
Output
The constructor name should be same as the class name
- Related Articles
- Should a constructor always have the same name as the class in java?
- Why the java file name should be always the same as a public class name?
- Can we define a method name same as class name in Java?
- Can a method have the same name as the class?
- Using predefined class name as Class or Variable name in Java
- What is the difference between simple name, canonical name and class name in a Java class?
- Can we define multiple methods in a class with the same name in Java?
- Display the package name of a class in Java
- Get the unqualified name of a class in Java
- Get the class name for various objects in Java
- Get the fully-qualified name of a class in Java
- Is there any advantage to using __construct() instead of the class's name for a constructor in PHP?
- Get the fully-qualified name of an inner class in Java
- Why the name of cell is cell?
- Get Canonical Name for a class in Java

Advertisements