- Trending Categories
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
Can constructors be marked final, abstract or static in Java?
Except public, protected and, private constructor does not allow any other modifier.
When you use a final keyword with a method or constructor it cannot be overridden. But, a constructor in Java cannot be overridden therefore, there is no need of using the final keyword with the constructor.
Since you cannot override a constructor you cannot provide body to it if it is made abstract. Therefore, you cannot use abstract keyword with the constructor.
If you want to invoke a member of a class before instantiating the class you need to use static before it. But, contractors are called implicitly at the time of instantiation. Therefore, there is no point in using static with constructors.
Example
public class Sample { public final static Sample(){ System.out.println("Hello"); } public static void main(String args[]) throws Exception{ Sample obj = new Sample(); } }
Output
C:\Sample>javac Sample.java Sample.java:2: error: modifier static, final not allowed here public final static Sample(){ ^ 1 error
Advertisements