
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can we define a static constructor in Java?
No, we cannot define a static constructor in Java, If we are trying to define a constructor with the static keyword a compile-time error will occur.
In general, static means class level. A constructor will be used to assign initial values for the instance variables. Both static and constructor are different and opposite to each other. We need to assign initial values for an instance variable we can use a constructor. We need to assign static variables we can use Static Blocks.
Example
public class StaticConstructorTest { int x = 10; // Declaratiopn of Static Constructor static StaticConstructorTest() { System.out.println("Static Constructor"); } public static void main(String args[]) { StaticConstructorTest sct = new StaticConstructorTest(); } }
In the above example, we have created a static constructor. the code doesn't compile and it can throw an error says that modifier static not allowed here.
Output
StaticConstructorTest.java:4: error: modifier static not allowed here
- Related Articles
- Can we initialize static variables in a default constructor in Java?
- Can we define constructor inside an interface in java?
- Can we define a parameterized constructor in an abstract class in Java?
- Why can't we define a static method in a Java interface?
- Can we define constant in class constructor PHP?
- Can we have a constructor private in java?
- Can we declare a constructor as private in Java?
- Can we serialize static variables in Java?
- Can we declare constructor as final in java?
- Can we call a constructor directly from a method in java?
- Can we make static reference to non-static fields in java?
- Can we override the static method in Java?
- Can we override a private or static method in Java
- Can we declare a static variable within a method in java?
- Can we use "this" keyword in a static method in java?

Advertisements