- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- Can we declare an abstract method final or static in java?
- Can a class in Java be both final and abstract?
- Why can't static method be abstract in Java?
- Why can't a Java class be both abstract and final?
- Can constructors be inherited in Java?
- Final static variables in Java
- Why should a blank final variable be explicitly initialized in all Java constructors?
- Static and non static blank final variables in Java
- Initializer for final static field in Java
- Difference Between Static and Final in Java
- Can a final class be subclassed in Java?
- Can a constructor be made final in Java?
- What are final, abstract, synchronized non-access modifiers in Java?
- Assigning values to static final variables in java
- What is static blank final variable in Java?

Advertisements