

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- How can create a table having the name like a^b along with same column name? name?
- Aggregating rows in SAP ABAP with the same name
- Get the fully-qualified name of an inner class in Java
- MySQL query to display columns name first name, last name as full name in a single column?
Advertisements