Java Internal classes

Source: Internet
Author: User
Tags modifiers



1. Inner class is divided into member inner class, Static nested class, Method inner class, anonymous inner class.

The commonality of several inner classes:

A, the inner class is still a separate class, after compiling the inner class will be compiled into a separate. class file, but preceded by the class and $ symbols of the outer class.

B, the inner class cannot be accessed in a normal way. An inner class is a member of an external class, so the inner class is free to access the member variables of the outer class, whether private or not.

2, the member inner class: The form is as follows

Class Outer {
Class inner{}
}

Compiling the above code will result in two files: Outer.class and Outer$inner.class.

No static declarations are allowed within a member's inner class! The following code cannot be compiled.

Class inner{
static int a = 10;
}

The only way to access a member's inner class is through an object of an outer class!

A, instantiate an inner class object from a non-static method of an external class.

     class Outer {
       private int i = ten;
       public void Makeinner () {
           inner in = new Inner (); br>            in.seeouter ();
       }
       class inner{
           public void Seeouter () {
&nbs P              system.out.print (i);
           }
       }
   }

On the surface, we instantiate the inner class object without creating an object of the outer class, and contradict the above words. In fact, it is not possible to call the Makeinner () method without creating an external class object, so you will end up creating an external class object. You may try to decorate the Makeinner () method as a static method, that is, static public void Makeinner (). This allows you to instantiate a class without creating an external category! But is it possible to access non-static members and methods in a static method? Obviously not. It does not have this reference. I can't jump out of that rule! But what if an external class object is instantiated in this static method, and then the object is instantiated in the same instance? Absolutely! That is, the next item.

B, instantiate the inner class object from the static method of the outer class.

     class Outer {
       private int i = ten;
       class inner{
           public void Seeouter () {
  & nbsp            system.out.print (i);
           }
       }
       public static void Main (string[] args) {
           oute R out = new Outer ();
           outer.inner in = Out.new Inner ();
           //outer.inner in = new Outer (). New Inner ();
           in.seeouter ();
       }
   }

The line that is commented out is the combined form of the two lines above, a concise statement.

In contrast: Instantiating an inner class object in a non-static method of an external class is a normal new way: Inner in = new Inner ();

To instantiate an inner class object in a static method of an external class, you must first create an outer class object:

Outer.Inner in = new Outer (). New Inner ();

C, the This reference of the inner class.

A normal class can refer to the current object with this, as is the inner class. But what if the inner class wants to refer to the current object of the outer class? Use "External class name". This is the form of the following example of Outer.this.

Class Outer {
Class inner{
public void Seeouter () {
System.out.println (this);
System.out.println (Outer.this);
}
}
}

D, the modifier of the member's inner class.

For normal classes, the available modifiers are final, abstract, strictfp, public, and default package access.

But the member inner class is more like a member variable and method.

Available modifiers are: final, abstract, public, private, protected, STRICTFP, and Static. Once the inner class is modified with static, it becomes a static inner class.

3, the method inner class.

As the name implies, put the class inside the method.

Class Outer {
public void dosomething () {
Class inner{
public void Seeouter () {
}
}
}
}

A, the method inner class can only be instantiated within a method that defines the inner class, and it cannot be instantiated outside of this method.

B, the method inner class object cannot use the non-final local variable of the method in which the inner class resides.

Because the local variable of the method is on the stack, it only exists within the lifetime of the method. When a method ends, its stack structure is deleted, and local variables become history. But after the method finishes, the inner class object created inside the method may still exist in the heap! For example, if a reference to it is passed to some other code and stored within a member variable. Because there is no guarantee that the lifetime of local variables is as long as the inner class objects of the method, they cannot be used by inner class objects.

     class Outer {
       public void DoSomething () {
           final int a =10;class inner{public void Seeouter () {
                   SYSTEM.OUT.PRINTLN (a);
               }
           }
           inner in = new Inner ();
           in.seeouter ();
       }
       public static void Main (string[] args) {
           oute R out = new Outer ();
           out.dosomething ();
       }
   }

C, the modifier for the inner class of the method.

Unlike a member inner class, a method inner class is more like a local variable. Only final and abstract can be used to decorate the inner class of a method.

D, method inner class within static method.

Static methods do not have this reference, so internal classes within static methods suffer the same treatment: only static members of external classes can be accessed.

4, anonymous internal class.

As the name implies, there are no names inside the class. On the surface they seem to have names, which are not their names.

     class Car {
       public void Drive () {
           SYSTEM.OUT.PRINTLN ("Driving a car!");
       }
   }
   
   class test{
       public static void Main (string[] args) {
           car car = new car () {
              &NBSP ;p ublic Void Drive () {
                   SYSTEM.OUT.PRINTLN ("Driving Another car! ");
               }
           };
           car.drive ();
       }
   }

The result is output: Driving another car! The car reference variable is not a reference car object, but an object of the car anonymous subclass.

The key to establishing an anonymous inner class is to override one or more methods of the parent class. Again, the method of overriding the parent class is not the creation of a new method. Because a reference to the parent class cannot call a method that the parent class does not own! Creating a new method is superfluous. In short, refer to polymorphism.

B, an anonymous internal class of interfaces.

     interface  vehicle {
       public void Drive ();
   }
   
   class test{
       public static void Main (string[] args) {
           vehicle v = new Vehicle () {
               public void Drive () {
                   SYSTEM.OUT.PRINTLN ("D Riving a car! ");
               }
           };
           v.drive ();
       }
   }

The code above is strange, as if it were instantiating an interface. This is not the case, the anonymous inner class of an interface is an anonymous class that implements an interface. And only one interface can be implemented.

C, an anonymous inner class of parameters.

     class bar{
       void Dostuff (Foo f) {}
   }

   interface foo{
       void Foo ();
   

   CL test{
       static void Go () {
           bar b = new Bar ();
           b.dostuff (new Foo () {
              &N bsp;public void foo () {
                 system.out.println   ("Foofy") );
               }
           });
       }
   }

5, static nested class.

Technically, a static nested class does not belong to an inner class. Because an inner class shares a special relationship with an external class, it is more specifically a shared relationship to an instance. Static nested classes do not have the above relationship. It is only positioned inside another class and is therefore also referred to as a top-level nested class. The static meaning is that the inner class can access it just like any other static member without an external class object. Static nested classes cannot access members and methods of external classes.

Class outer{static Class inner{}} class Test {public static void main (string[] args) {Ou ter.        Inner n = new Outer.Inner (); }    }


Java Internal classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.