Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java prohibit static fields in inner classes?

class OuterClass {  class InnerClass {   static int i = 100; // compile error   static void f() { } // compile error  } }  

Although it's not possible to access the static field with OuterClass.InnerClass.i, if I want to record something that should be static, e.g. the number of InnerClass objects created, it would be helpful to make that field static. So why does Java prohibit static fields/methods in inner classes?

EDIT: I know how to make the compiler happy with static nested class (or static inner class), but what I want to know is why java forbids static fields/methods inside inner classes (or ordinary inner class) from both the language design and implementation aspects, if someone knows more about it.

like image 600
Jichao Avatar asked Dec 23 '09 15:12

Jichao


People also ask

Why can't inner classes have static members?

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Can we declare static variable in inner class?

InnerClass cannot have static members because it belongs to an instance (of OuterClass) and a static members means that this variable belongs to the entire class and not any particular instance.

Why we should not use static variables in Java?

Static variables are generally considered bad because they represent global state and are therefore much more difficult to reason about. In particular, they break the assumptions of object-oriented programming.

Do inner classes need static?

The class which enclosed nested class is known as Outer class. In the Java programming language, you can not make a top-level class static. You can only make nested classes either static or non-static. If you make a nested class non-static then it also referred to as Inner class.


1 Answers

what I want to know is why java forbids static fields/methods inside inner classes

Because those inner classes are "instance" inner classes. That is, they are like an instance attribute of the enclosing object.

Since they're "instance" classes, it doesn't make any sense to allow static features, for static is meant to work without an instance in the first place.

It's like you try to create a static/instance attribute at the same time.

Take the following example:

class Employee {     public String name; } 

If you create two instances of employee:

Employee a = new Employee();  a.name = "Oscar";  Employee b = new Employee(); b.name = "jcyang"; 

It is clear why each one has its own value for the property name, right?

The same happens with the inner class; each inner class instance is independent of the other inner class instance.

So if you attempt to create a counter class attribute, there is no way to share that value across two different instances.

class Employee {     public String name;     class InnerData {         static count; // ??? count of which ? a or b?       } } 

When you create the instance a and b in the example above, what would be a correct value for the static variable count? It is not possible to determine it, because the existence of the InnerData class depends completely on each of the enclosing objects.

That's why, when the class is declared as static, it doesn't need anymore a living instance, to live itself. Now that there is no dependency, you may freely declare a static attribute.

I think this sounds reiterative but if you think about the differences between instance vs. class attributes, it will make sense.

like image 65
OscarRyz Avatar answered Sep 17 '22 13:09

OscarRyz