tags:

views:

197

answers:

2

I am trying to find out the reasons 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);
    }
}
+15  A: 

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.)

Kevin Conner
Pretty much spot-on. An inner class is by default not static, which is to say that each instance of a class that has a nested class gets its own copy of the nested class. This is how you can reference "this" in the class, and why you can't declare static methods or fields, doesn't make any sense, since each instance of the outer class has its own copy.A "static" nested class (by definition a true "inner" class) means there is only one copy of that class, which is much more like a regular class.I always thought that the semantics of these nested classes were hopelessly convoluted.
Jeff Walker
Also I bent the truth about non-static methods existing once per instance, they actually don't from a memory standpoint. I was just making a conceptual point.
Kevin Conner
@jeff, what is the difference between a statically declared inner class and a non static one in terms of object instantiation from that class? is there some sort of, you can only instantiate a single object from a static inner class, and can instantiate more than one object from a non-static inner class? is that it?
ultrajohn
For variables, it's not "once ever", but once per Class. Since a Class can loaded multiple by different class loaders, it's statics can exists multiple, though in separate namespaces.
Software Monkey
@Monkey Cool, I didn't know that.
Kevin Conner
@ultrajohn If the inner class is static, it's just a class that's declared in another class, so you can instantiate it normally. Otherwise, you have to have an outer class instance to do it, because the new instance will get to use its members. The full syntax is "outerClassInstance.new InnerClass(args)". But if "this" is an instance of the outer class, then you can just say "new InnerClass(args)", and it will associate the instance with "this".
Kevin Conner
@kevin, ok, am clarified... btw, i find the syntax weird, really... tnx guys...
ultrajohn
It does look weird! Normally "new" only comes at the beginning of things. But much like "A.B" refers to the B that belongs to A, "outer.new Inner()" refers to outer's version of instantiating Inner. That is, outer is not an argument to the constructor Inner() so much as it is the thing that does the instantiating. Conceptually.
Kevin Conner
+5  A: 

In addition to Kevin Conner's response, 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 static 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);
        }
    }
edwardTheGreat
Good insight, that is probably what he was trying to do come to think of it.
Kevin Conner