classe intérieure Java et classe statique nichée

Mots clés : javainner-classesstatic-classesjava

meilleur 4 Réponses classe intérieure Java et classe statique nichée

vote vote

100

OuterClass.StaticNestedClass 
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); 
class OuterClass {     ...     class InnerClass {         ...     } } 
OuterClass outerObject = new OuterClass() OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 
class A {   int t() { return 1; }   static A a =  new A() { int t() { return 2; } }; } 
vote vote

85

package pizza;  public class Rhino {      ...      public static class Goat {         ...     } } 
package pizza;  public class Rhino {      public class Goat {         ...     }      private void jerry() {         Goat g = new Goat();     } } 
Rhino rhino = new Rhino(); Rhino.Goat goat = rhino.new Goat(); 
public class Rhino {      private String barry;      public class Goat {         public void colin() {             System.out.println(barry);         }     } } 
new *ParentClassName*(*constructorArgs*) {*members*} 
new *InterfaceName*() {*members*} 
vote vote

74

public class Container {     public class Item{         Object data;         public Container getContainer(){             return Container.this;         }         public Item(Object data) {             super();             this.data = data;         }      }      public static Item create(Object data){         // does not compile since no instance of Container is available         return new Item(data);     }     public Item createSubItem(Object data){         // compiles, since 'this' Container is available         return new Item(data);     } } 
// class version 49.0 (49) // access flags 33 public class Container$Item {    // compiled from: Container.java   // access flags 1   public INNERCLASS Container$Item Container Item    // access flags 0   Object data    // access flags 4112   final Container this$0    // access flags 1   public getContainer() : Container    L0     LINENUMBER 7 L0     ALOAD 0: this     GETFIELD Container$Item.this$0 : Container     ARETURN    L1     LOCALVARIABLE this Container$Item L0 L1 0     MAXSTACK = 1     MAXLOCALS = 1    // access flags 1   public <init>(Container,Object) : void    L0     LINENUMBER 12 L0     ALOAD 0: this     ALOAD 1     PUTFIELD Container$Item.this$0 : Container    L1     LINENUMBER 10 L1     ALOAD 0: this     INVOKESPECIAL Object.<init>() : void    L2     LINENUMBER 11 L2     ALOAD 0: this     ALOAD 2: data     PUTFIELD Container$Item.data : Object     RETURN    L3     LOCALVARIABLE this Container$Item L0 L3 0     LOCALVARIABLE data Object L0 L3 2     MAXSTACK = 2     MAXLOCALS = 3 } 
OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 
new InnerClass(outerObject) 
vote vote

60

public class OuterClass {     public static class Inner1 {     }     public class Inner2 {     } } 
OuterClass outer = new OuterClass(); OuterClass.Inner2 inner = outer.new Inner2(); 
OuterClass.Inner2 inner = new OuterClass().new Inner2(); 
OuterClass.Inner1 nestedObject = new OuterClass.Inner1(); 

Questions similaires