- 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
Is it possible to create static constructor in java?
A constructor is used to initialize an object when it is created. It is syntactically similar to a method. The difference is that the constructors have same name as their class and, have no return type.
There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.
Static constructors
No, we cannot create a Static constructor in java You can use the access specifiers public, protected & private with constructors.
If we try to use static before a constructor a compile time error will be generated saying “modifier static not allowed here”.
Example
In the following Java example, we are trying to create a static constructor.
public class Student { public String name; public int age; public static Student(){ System.out.println("Constructor of the Student class"); } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static void main(String args[]) { new Student().display(); } }
Compilation error
On compiling the above program generates the following error −
Student.java:6: error: modifier static not allowed here public static Student(){ ^ 1 error
- Related Articles
- Is it possible to use this keyword in static context in java?
- How to call a static constructor or when static constructor is called in C#?
- Is there any alternative solution for static constructor in java?
- Is it possible to create a class without a name in Java?
- Can we define a static constructor in Java?
- What is a static constructor in C#?
- Is it possible to create a custom exception in java without extending Exception class?
- How to create a parameterized constructor in Java?
- How to create a default constructor in Java?
- Difference between Static Constructor and Instance Constructor in C#
- Can we initialize static variables in a default constructor in Java?
- Do static variables get initialized in a default constructor in java?
- Is it possible to instantiate Type-parameter in Java?
- Is it possible to create a new data type in JavaScript?
- How to create static VarHandle in Java 9?

Advertisements