0

I have come to know that inner classes in java cannot have static members unless and until they are declared final . But shouldn't it be theoretically possible to declare static members in a local class ?

Eg.

public class OuterClass
{
  class innerClass
  {
    public static int i = 1;
  // inner class code goes here
  }
}

I know that standard initialisation of inner class occurs like this :

OuterClass outerClass = new OuterClass();
OuterClass.innerClass innerclassInstance = outerClass.new innerClass();

But why not this way??

OuterClass outerClass = new OuterClass();
outerClass.innerClass innerclassInstance = new outerClass.innerClass();

When ever an instance of OuterClass is created , it also contains a copy of innerClass. So if static(just static but not final ) members were allowed to be present in innerClass , then they should be( or can be) accessed this way

outerClassInstance.innerClass.member

Am I thinking in the wrong direction ? Is there any particular reason why inner classes were designed in the way they are ?

Hope I am clear . Sorry for my poor English.

krishna
  • 23
  • 2
  • 5
  • Because a static member does never depend on an instance. – csabinho Oct 20 '19 at 00:22
  • I know that a static variable in a class should (and will ) never depend on the instance of that class . But I did not understand why a static variable in the inner class should not depend on the instance of the enclosing class (or outer class).. @csabinho could you please elaborate.. Thanks in advance – krishna Oct 20 '19 at 00:44
  • Well, why should it? Could you give me any example where the static variable of an inner class depends on an instance of the outer class? – csabinho Oct 20 '19 at 00:46
  • If it does then it is possible for all instances of inner class to have a common variable which they can modify and use that variable for some purpose (like synchronization) with out touching the fields in the outer class which makes the code more compact . Isn't it ? – krishna Oct 20 '19 at 00:54
  • I don't really get it. Could you edit a code example into your question? – csabinho Oct 20 '19 at 00:57

1 Answers1

1

But shouldn't it be theoretically possible to declare static members in a local class ?

An inner class is implicitly associated with an object of its outer class, so the existence of the InnerClass class depends completely on each of the enclosing objects of OuterClass class.

Let's imagine, you don't declare 'i' as final, and you create the instances a and b of OuterClass class, the two instances are independent of each other. We can set:

OuterClass a = new OuterClass();
OuterClass.InnerClass a1 = a.new InnerClass();
a1.i = 9;

OuterClass b = new OuterClass();
OuterClass.InnerClass b1 = b.new InnerClass();
b1.i = 18;

Hmm, a static variable with two different values, exists in parallel, are independent of each other (because a and b are independent of each other).

It cannot happen because static variables are, essentially, global variables. All instances of the class InnerClass must share the same static variable.

But why not this way??

OuterClass outer = new OuterClass(); //I modified the variable name to avoid confusion
outer.InnerClass innerclassInstance = new outer.InnerClass();

By convention,

Type name = new Type();

, so outer (a name) can not be resolved to a type.

You can do this way:

OuterClass.InnerClass innerclassInstance = new OuterClass().new InnerClass();

Hope my answer may help you.

  • Thanks for the answer . Why shouldn't you allow a1.i and b1.i to have different values as they are from different environments(here a and b) and are independent . – krishna Oct 20 '19 at 02:27
  • Here, because static variables are, essentially, global variables. All instances of the class InnerClass must share the same static variable. The our initial hypothesis leads to a conclusions that contradict the definition of 'static '. – Do Van Tuan Oct 20 '19 at 02:36
  • I just found this post: https://stackoverflow.com/questions/1953530/why-does-java-prohibit-static-fields-in-inner-classes. You can refer to for better understanding. – Do Van Tuan Oct 20 '19 at 02:43
  • I have gone through the link . My question is a duplicate and I should have searched enough for previous questions before asking a question. Anyway Thank you so much @Do Van Tuan . I believe you have given the best possible answer . – krishna Oct 20 '19 at 02:55
  • It is my pleasure to help you. – Do Van Tuan Oct 20 '19 at 02:59