- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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
Advertisements