Java Internal classes

Source: Internet
Author: User

In general, there are 4 inner classes: General inner class, static inner class, local inner class, anonymous inner class.

I. GENERAL internal classes: Normal Inner classes are not static decorated and defined in the outer class body.
1. Methods in the general inner class can directly use instance variables and instance methods of the outer class.
2. You can create objects directly from internal classes in a regular inner class
3. The code is as follows:
public class Myouter {
private int x = 100;

Create an inner class
Class Myinner {
Private String y = "hello!";

public void Innermethod () {
System.out.println ("String =" + Y in the inner Class);
System.out.println ("x =" + X in the outer class);//Direct access to an instance variable in an external class X
Outermethod ();

System.out.println ("x is" + myouter.this.x);
}

}

public void Outermethod () {
x + +;

}

public void Makeinner () {
Creating an inner class instance in an external class method
Myinner in = new Myinner ();
}

/**
* @param args
*/
public static void Main (string[] args) {

Myouter mo = new Myouter ();
Creating MO objects using external class construction methods
Myouter.myinner inner = mo.new myinner ();//Regular inner classes need to be created with an instance of an external class to create an object, and an instance variable needs to access a similar
Create a Inner object
Inner.innermethod ();
TODO auto-generated Method Stub

}

}
4. Operation Result:

String =hello! in inner class
x =100 in the outer class
X is101

Two. Static inner class: Similar to other members of the class, you can decorate the inner class with static, such that the class is called a static inner class. A static inner class is similar to a static internal method that accesses only the static members of an external class, cannot access an instance variable of an external class directly, and an instance method, only through object references.
3. Because the static inner class does not have any references to external class instances, the This keyword cannot be used in static internal classes to access instance members in external classes, but static members in external classes can be accessed. This is with the static method of the general class figured out
4. Example code:
Package Com.m.ou;

public class Myouter {
public static int x=100;
public static Class myinner{
Private String y= "hello!";
public void Innermethod () {
System.out.println ("x=" +x);
System.out.println ("y=" +y);



}


}

/**
* @param args
*/
public static void Main (string[] args) {
Myouter.myinner si=new Myouter.myinner ();//static inner classes can create objects without external instances, and class variables can be accessed by class names similar to
Si.innermethod ();
TODO auto-generated Method Stub

}

}
4. Operation Result:
x=100
Y=hello!
Three. Local inner class: A class that is defined inside a method body or statement block (including methods, construction methods, local blocks, or static initialization blocks) becomes a local inner class.
A local inner class cannot add any access modifiers because it is only valid for local blocks.
1. The local inner class is only valid in the method body and, like the local variable you want to define, you cannot create an object of a local inner class outside the defined method body
2. When defining classes within a method, you should be aware of the following issues:
1. The method defines the local internal similarity method to define a local variable, cannot be decorated with access modifiers such as private, protected, public, or use static adornments, but can use final and abstract adornments
2. An inner class in a method can access an external class member. For parameters and local variables of a method, you must have a final decoration to access it.
The inner class defined in the 3.static method can access the static member defined by the outer class
3. Program code:
public class Jubu {
private int size=5,y=7;
Public Object makeinner (int localVar) {
final int finallocalvar=localvar;
Creates an inner class that is valid only in the Makeinner () method, just like a local variable. Cannot create an object of the Myinner class outside the method body
Class myinner{
int y=4;
Public String toString () {
Return "Outersize:" +size+
"\nfinallocalvar" + "" + "this.y=" +THIS.Y;


}

}



return new Myinner ();



}
}

Class main{/**
* @param args
*/
public static void Main (string[] args) {

Object Obj=new Jubu (). Makeinner (47);//Create a Jubu object, obj, and call its Makeinner () method, which returns a
The method returns an object of type Myinner, obj, and then calls its same tostring method.
System.out.println (Obj.tostring ());
TODO auto-generated Method Stub

}


}
4. Operation Result:

Outersize:5
Finallocalvar this.y=4
Four. Anonymous inner class: The ultimate purpose of defining a class is to create an instance of a class, but if an instance of a class is used only once, you can put the definition of the class in conjunction with the creation of the class, or create a class at the same time that the class is defined
A class with no name defined in this way becomes an anonymous inner class.
The general format for declaring and constructing anonymous inner classes is as follows:
New Classorinterfacename () {

/* Class Body */}

1. An anonymous inner class can inherit a class or implement an interface, where the Classorinterfacename is the class name inherited by the anonymous inner class or the interface name of the implementation. But anonymous inner classes cannot implement an interface and inherit a class at the same time, nor can they implement multiple interfaces. If an interface is implemented, the class is a direct subclass of the object class, the anonymous class inherits a class or implements an interface, and the extends and implements keywords are not required.

2. Because the anonymous inner class does not have a name, the construction method cannot be defined in the class body, and an instance of the class cannot be created because the class name is not known and the keyword cannot be used. In fact, the definition, construction, and first use of anonymous inner classes occur in the same place. In addition, the upper formula is an expression that returns a reference to an object, so you can use it directly or copy it to an object variable. Cases:


TypeName obj=new Name () {

   /* Here is the class body */
      }
   Similarly, you can use the constructed object as a parameter of the call. Example:
   
   somemethod (New Name () {
   /* here is the class body */   });
   3. Program code:
  public class Niming {
 private int size=5;
 public Object makeinner (int localVar) {
  final int finallocalvar=localvar;
  return New Object () {
   //uses an anonymous inner class
   public String toString () {
    return "outersize=" +size+ "\nfinallocalvar=" +finallocalvar;
    
    
   }
   
   
  };
  
  
 }

/**
* @param args
*/
public static void Main (String args[])
{
Object obj=new niming (). Makeinner (47);
System.out.println (Obj.tostring ());



}

}
4. Program Operation Result:
Outersize=5
finallocalvar=47

from:http://blog.csdn.net/qq435757399/article/details/7037007

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.