9

Consider the following code

class A {
    static class B{
        int a = 0;
    }
    public static void main(String argc[]) {
        B var1 = new B();
        B var2 = new B();
        var1.a = 5;
        var2.a = 6;
        System.out.println(var1.a+" and "+var2.a);
    }
}

It outputs 5 and 6. Static members are loaded only once.But the output contradicts with that statement.So surely the concept of static classes is different from static data members.So what does static mean in case of static classes

Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90

3 Answers3

11

A copy paste from oracle:

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();

An example:

There is no need for LinkedList.Entry or Map.Entry to be top-level class as it is only used by LinkedList aka Map. And since they do not need access to the outer class members, it makes sense for it to be static - it's a much cleaner approach.

Frank
  • 16,476
  • 7
  • 38
  • 51
3

You've misunderstood the concept. B is a static class with an int a attribute. In your code, you're creating two instances of the B class and each instance has its own a attribute with its value 5 and 6 respectively. Don't confuse the static class with the static attribute/method of a class.

The behavior you're trying to get can be done if you add the static modifier to the a attribute on the B class. Otherwise, your code it's like this:

class B{
    int a = 0;
}

class A {
    public static void main(String argc[]) {
        B var1 = new B();
        B var2 = new B();
        var1.a = 5;
        var2.a = 6;
        System.out.println(var1.a+" and "+var2.a);
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 1
    @JanDvorak AFAIK this answer the question. `B` is a static class but its behavior is not like a `static` attribute. – Luiggi Mendoza Nov 04 '12 at 20:31
  • 1
    The question was, "what is a static class", not "how do I achieve the expected output" – John Dvorak Nov 04 '12 at 20:31
  • 1
    How can an answer to "So what does static mean in case of static classes" start with "You're wrong?" – Dan Rosenstark Nov 04 '12 at 20:34
  • @JinuJD Is Frank's answer good for you or should I try to write one? – John Dvorak Nov 04 '12 at 20:34
  • @Yar because OP is confusing the terms. I haven't add the `static class` definition but went to explain why the codes doesn't behave as OP though. Is that wrong? – Luiggi Mendoza Nov 04 '12 at 20:36
  • 2
    @JanDvorak maybe this is not a direct answer but explains the misconception on OP about static classes vs static attributes. – Luiggi Mendoza Nov 04 '12 at 20:38
  • @Jan Dvorak:I got the concept from that answer.." a static nested class is behaviorally a top-level class "... – Jinu Joseph Daniel Nov 04 '12 at 20:46
  • @LuiggiMendoza OP doesn't want to be corrected - he wants to be taught. There's no myth to bust, only blanks to fill. – John Dvorak Nov 04 '12 at 20:46
  • @LuiggiMendoza Note that you've collected +3/-3 - this means I'm not alone in my opinion. – John Dvorak Nov 04 '12 at 20:49
  • @JanDvorak and I'm not alone in my answer :). The important thing here is to show OP the right way, and Frank did it, still my answer can help OP about the code sample behavior. – Luiggi Mendoza Nov 04 '12 at 20:51
  • The OP is wrong in considering the static class to be a static member that is only loaded once. – user207421 Nov 04 '12 at 21:51
  • @LuiggiMendoza if it's "not a direct answer" then it should be a comment on the question. And instead of "you're wrong" try something like, "it seems you've confused the terms." See http://stackoverflow.com/faq#etiquette. "Nice" es subjetivo en inglés, pero es lo mismo que entendés por "tratar a los demás con el máximo de respeto incluso si no entienden un pimiento" en tu idioma ;) – Dan Rosenstark Nov 04 '12 at 23:32
  • 1
    @Yar okay, fixed the way the answer started. And not, this should be an answer, not a comment. Por cierto, no creo haber faltado el respeto a nadie con mi respuesta ni mis comentarios, pero en caso que lo hubiese hecho favor de indicármelo para corregirlo de inmediato :). – Luiggi Mendoza Nov 05 '12 at 00:22
  • All good, thanks for fixing that and good luck here on SO, salu2! – Dan Rosenstark Nov 05 '12 at 14:46
3

Static, in case of classes, means that they are not related to an instance of their outer class:

class A{
  class B{
    ...
  }
}
...
new A.B(); //error

is invalid. Because B is not static, it holds an implicit reference to an instance of A. This means you cannot create an instance of B without an instance of A.

class A{
  static class B{
    ...
  }
}
...
new A.B();

is perfectly valid. Since B is static, it doesn't hold a reference to A, and can be created without an instance of A existing.

Static class is a class that doesn't hold an implicit reference to its enclosing class. Static class behaves just like an ordinary class except its namespace being within another class.

Non-static inner class holds an implicit reference to its enclosing class. The enclosing class' variables are directly accessible to an instance of the inner class. A single instance of the outer class can have multiple instances of its inner class(es).

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
  • It became more clear!...Thanks for the answer..So it's just a way of packaging classes – Jinu Joseph Daniel Nov 04 '12 at 20:50
  • 1
    @downvoter. At least explain why downvoting (that really pisses me off, it's like throwing a stone and hiding the hand). Your answer clarifies what Frank explained, it would be good to add an example. – Luiggi Mendoza Nov 04 '12 at 20:54