Java scope rules and inner classes

All crazy Java coverage rules make my head spin, and the pointlessness of public static void doesn't help. So far, all the programming languages โ€‹โ€‹that I have used are either the lexical scope, or some approximation to it without access modifiers, i.e. The inner material captures the outer material and has access to the outer material as long as the inner material exists.

So, how do I understand the scope rules for inner classes in Java? Do they gain access to variables declared in the outer class, or are there some weird edge cases that I have to worry about because all public static private things are floating around?

+4
source share
5 answers

Static nested classes 1 exactly correspond to outer classes, except that they have access to all members of the outer class, regardless of access qualifier. They exist separately from any instance of the outer class, so you need a reference to the instance to access any instance variables or non-stationary methods of the outer class.

Non-static nested classes (called inner classes) arise only in the context of an instance of an outer class. When building, they automatically generate a second this field, with which you can access from the inner class using the Outer.this syntax. Each instance of the inner class is enclosed in one instance of the outer class. Again, all permissions of static nested classes apply to inner classes. But since they already have an instance of the outer class, it can automatically access instance variables and methods of the outer class.

For a pleasant (and very detailed) discussion of inner classes and access specifiers, you can read the inner class specification . It describes, among other things, how a nested class gains access to private members of its outer class (s). Softer reading is a Nested Class Tutorial .

Disable topic: suppose you have this class structure:

 public class O { public O() { ... } public class I { // an inner class public I() { ... } ... } ... } 

and you created an instance of O :

 O outer = new O(); 

Now suppose you want to instantiate an OI . you cannot just use new OI() because the new instance of I must be enclosed in a specific instance of O For this, Java provides the following syntax:

 OI inner = outer.new OI(); 

Then inner will have a second this field specified as outer .

Note that this "qualified syntax for the new operator is only used for inner classes; it would not be necessary (essentially an error) if I were a nested static class.

1 You often come across the phrase โ€œstatic inner classโ€ (including, embarrassingly, in an earlier version of this answer). This is the wrong terminology. In Java, "inner classes" are specially non- static nested classes.

+17
source

You must be different:

  • Static inner classes have access to all static members outside their declaration.
  • Inner instance classes have access to all members of the class outside their declaration and to the final fields in the function in which they are declared.

Keep in mind that a non-static inner class also has a hidden variable with an instance of the outer class to access members. And that all referenced final fields (therefore, they must be final) are copied into the inner class into other hidden member variables when creating an instance of the inner class.

Example:

 public void doStuff(final int a, int b) { final int c; // Can be referenced int d; // Cannot be referenced, not final executer.execute( new Runnable() { public void run() { System.out.println("a: "+a+" c: "+c); } } b++; // Not final, not referencable System.out.println(b); } 
+3
source

I don't know if this helps, but from the java tutorials :

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static methods of a class, a static nested class cannot refer directly to instance variables or methods defined in its encompassing class , it can only use them through an object reference. Note. A static nested class interacts with the members of an instance of its outer class (and other classes), like any other top-level class . In essence, a static nested class is behaviorally a top-level class that has been nested in another top-level class for ease of packaging.

Inner Classes [Non-Static Nested class?]

As with instance methods and variables, an inner class is associated with an instance of its enclosing class, and has direct access to these object methods and fields . Also, since the inner class is associated with an instance, it cannot define any static members.

You should check out the java tutorial on nested classes .

+3
source

Rules for the inner class

  • The outer class accessed by the inner class
  • The inner class cannot be accessed by the outer class
  • Internal class members used only methods and class members, but only access to filled information
0
source

Method Hidden inner classes: - Access only to the final members of the outer class.

-1
source

Source: https://habr.com/ru/post/1337724/


All Articles