Reputation: 15960
I am trying to find why the class cant be created as a static? Like:
public static class Qwert {
public static void main(String args[]) {
int x = 12;
while (x<12) {
x--;
}
System.out.println(" the X value is : "+ x);
}
}
Upvotes: 27
Views: 47107
Reputation: 2044
To prevent a particular class being instantiated you should add a private Constructor. This stops 'any other' Class from being able to create an object of type Qwert
.
for example:
public class Qwert {
private Qwert() {}
public static void main(String args[]){
int x = 12;
while(x<12){
x--;
}
System.out.println(" the X value is : "+ x);
}
}
Upvotes: 8
Reputation: 83
In Java, by definition, static applies to the inner components of a class. "X is static" means in Java "X is associated with the class in which it is defined, rather than with any instance of the class".
The word "static" means literally "fixed at one location in memory". Every instance of the class shares a static variable or static member. Hence the use of "class variable" as a synonym for "static variable". Which lets you see at once that you cannot define an outer class as static.
It therefore follows that your class, Qwert, cannot be created as static. Unless it is subsumed as a component of an outer class, effectively making it an inner class.
Upvotes: 0
Reputation: 4597
We should define members as static which
Now suppose we are defining an outer class as static and suppose we are allowed to do so. Will this serve any purpose or provide any advantage to a developer or it will create ambiguity and complications for both developers and language creators?
Let’s check, defining an outer class as static will serve purposes which we have defined above or not?
From above points, we can say Java creators had not allowed an outer class to be static because there is no need to make it static. Allowing to make the outer class static will only increase complications, ambiguity and duplicity. Read more on Why An Outer Java Class Can’t Be Static
Upvotes: 3
Reputation: 551
its because when we use static keyword for a component, that component becomes a class level component and its memory is taken by its class.
Upvotes: 1
Reputation: 1527
To prevent any class from creating an instance of Qwert
, either by inheritance or by using reflection, you make the constructor fail by placing a poison pill:
public class Qwert {
private Qwert() throws IllegalAccessException {
throw new IllegalAccessException("Utility class!");
}
public static class Yuiop {
public Yuiop() throws IllegalAccessException {
// generates a synthetic accessor method to super()
}
}
public static void main(String args[]) {
new Yuiop();
}
}
Upvotes: 1
Reputation: 8734
In Java, the static
keyword typically flags a method or field as existing not once per instance of a class, but once ever. A class exists once anyway so in effect, all classes are "static" in this way and all objects are instances of classes.
static
does have a meaning for inner classes, which is entirely different: Usually an inner class instance can access the members of an outer class instance that it's tied to, but if the inner class is static
, it does not have such a reference and can be instantiated without an instance of the outer class. Maybe you saw that someplace, then tried to use it on a top-level class, where it isn't meaningful.
Or maybe you saw it in other languages like C#, whose syntax is an awful lot like Java's.
(One time I couldn't figure out why an outer class instance wasn't being garbage-collected -- it was because I was keeping a reference to one of its inner class instances elsewhere, and the inner class was not static
and so had a reference to the outer class instance. So by default, I make inner classes static
now.)
Upvotes: 64