
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Should a Constructor Always Have the Same Name as the Class in Java
Yes, the constructor should always have the same name as the class.
Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class.
If the programmer doesn't write a constructor the compiler writes a constructors on his behalf.
Example
public class Sample{ public Sample(){ System.out.println("This is a constructor"); } public static void main(String args[]){ Sample obj = new Sample(); } }
Output
This is a constructor
Advertisements