

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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 Questions & Answers
- 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 it possible to create a class without a name in Java?
- Is there any alternative solution for 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?
- Is it possible to create a new data type in JavaScript?
- Can we define a static constructor in Java?
- Is it possible to synchronize the string type in Java?
- Difference between Static Constructor and Instance Constructor in C#
- Is it possible to resume java execution after exception occurs?
- Is it possible to assign a reference to this (keyword) in java?
- How to create a parameterized constructor in Java?
- How to create a default constructor in Java?
- Is it possible to catch multiple Java exceptions in single catch block?
Advertisements