Java Internal Classes

Source: Internet
Author: User
Tags class definition

Why use internal classes?

1. Internal classes provide better encapsulation, only external classes can access internal classes

2. Inner classes can inherit an interface independently, independent of whether an external class inherits the interface

3. The properties and methods in an inner class are not directly accessible even by external classes, whereas internal classes can directly access the properties and methods of external classes, even if the private

4. Facilitates the writing of callback functions

In Java, the inner class is mainly divided into member inner class, method inner class, anonymous inner class, static inner class. Let's take a look at the static inner class and the member inner class.

Static Inner class:

The keyword static can modify member variables, methods, code blocks, in fact, it can also decorate the inner class, using static decorated inner class we call static inner class. There is one big difference between static inner and non-static inner classes, and we know that a non-static inner class implicitly holds a reference after compilation, which points to the perimeter in which it was created, but does not have a static inner class. Without this reference, it means:

    1. Static internal classes are created without the need to rely on the perimeter class and can be created directly

    2. Static inner classes are not allowed to use non-static member variables and methods of any perimeter class, while inner classes can

Combine the following code to deepen your understanding.

PublicClassouterclass1{
static variables
PrivateStaticString s1="A";
Member variables
PrivateString s2="B";
Static methods
PrivateStaticvoidM1 (){
System.Out.println ("Static ' s method M1 is excuted!");
}
Member Methods
Privatevoid M2 (){
System.Out.println ("Static ' s method m2 is excuted!");
}
/* public static void Main (String args[]) {
SYSTEM.OUT.PRINTLN ("SD");
} */
Static internal classes, which can be decorated with access control permissions, Public,protected,private, default
StaticClassinerclass{
Static methods in static inner classes
PublicStaticvoidM3 (){
System.Out.println (S1);Inner class method accesses external class private static variable
SYSTEM.OUT.PRINTLN (S2); Inner class method accesses external class Private member variable, error, cannot reference non-static variable from static context s2
M1 ();Internal classes access static methods of external classes
//m2 ();  //error, cannot refer to non-static method from static context m2 ()
 }
  //Member method in static inner class
  Public  void  M4 ( ) {
 }
 }
  public  Static  void  Main ( String args[]) {
    //External class method accesses static methods of static internal classes, access to static methods, direct class names. Method name Access
    OUTERCLASS1.INERCLASS.M3 ();
    //External class methods to access member methods of static inner classes, you need to create an object
   inerclass t= new outerclass1.inerclass ();
   T.M4 ();
 }
}
//output: A
//static ' s method M1 is excuted!

As you can see from the above code:

1. Static internal classes can be equivalent to static variables.

2. Static internal classes can access static data for external classes directly, including static variables and static methods, but cannot access member data (member variables and member methods).

member Inner class:

The member inner class is also the most common inner class, which is a member of the outer class, so he can access all the member properties and methods of the perimeter class without restrictions, although it is private, but the perimeter class accesses the member properties and methods of the inner class through an internal class instance.

There are two points to note in the members inner class:

    1. No static variables and methods can exist in a member's inner class

    2. The member inner class is dependent on the perimeter class, so only the perimeter class is created first to create the inner class

PublicClassouterclass2{
static variables
PrivateStaticString s1="A";
Member variables
PrivateString s2="B";
Static methods
PrivateStaticvoidM1 (){
System.Out.println ("Static ' s method M1 is excuted!");
}
Member Methods
PrivatevoidM2 (){
System.Out.println ("Static ' s method m2 is excuted!");
}
/* public static void Main (String args[]) {
SYSTEM.OUT.PRINTLN ("SD");
} */
member inner class, can be decorated with access control permissions, Public,protected,private, default
Classinerclass{
/* public static void M3 () {//member inner class not allowed to have static declaration
System.out.println (S1);
M1 ();
} */
Member method in //member inner class
  public  void  M4 () {
   system. Out.println ( "SDFSF");
 }
 }
  public  Static  void  main ( string[] args) {
    //Access member methods in members inner classes, M4 () method. The M4 () method is a member method, so Inerclass first creates the object. Inerclass is a member, so create the Outerclass object first.
     //Create an external class object
   outerclass2 t2= New outerclass2 ();
   inerclass inner=t2.new inerclass ();
   INNER.M4 ();
 }
} /span>

Need to remember:

1. The member inner class can be considered as a member variable

2. member inner class cannot have static declaration

3. The member inner class can access all the data of the external class

Local inner class:

The local inner class definition in the method of the outer class, and the member inner class basically consistent, only their scope is different, the local inner class can only be used in the method, out of this method will be invalidated. The use of this class is mainly to apply and solve more complex problems, want to create a class to assist our solution, then do not want this class is publicly available, so it produced a local inner class.

PublicClassouterclass3{
Method
PublicvoidM1 (){
FinalIntI=10;
Local inner class, local inner class cannot be decorated with access modifier
Classinnerclass{
public static void M1 () {} Inner class cannot have static declaration
//Member Method
    public  void  m2 () {
     system. Out.println (i);
   }
 }
 innerclass inner= new innerclass ();
 INNER.M2 ();
 }
  //Want to access the M2 () method in an external class, The M2 () method is not directly accessible in the local inner class, the scope is different, the curly braces do not recognize the
   public  static  void  Main ( string[) args) {
   OUTERCLASS3 t3= new  OUTERCLASS3 ();
   T3.M1 ();
 }
} /span>

For local inner classes, the following points need to be kept in mind:

1. Local internal classes are equivalent to local variables

2. Local inner classes cannot be decorated with access control permission modifiers

3. Local internal classes when accessing local variables, local variables must use the final decoration.

Anonymous inner class:

The anonymous inner class is actually a method inner class without a name, so it conforms to all the constraints of the method's inner class, and there are some places to be aware of:

    1. The anonymous inner class is not an access modifier.

    2. Anonymous inner classes must inherit an abstract class or implement an interface

    3. No static members or methods can exist in an anonymous inner class

    4. The anonymous inner class is not constructed because it does not have a class name.

PublicClasstesta{
PublicStaticvoidTCustomerService CS) {//t method Call Interface
   cs.logout ();
 }
  //Program entry
  public  Static  void  Main ( string[] args) {
  //Call T method
    //T (new CustomerService ());   Error, interface cannot create object
   t ( new customerservice () {   //new customerservice () {} Whole is anonymous inner class
    Public  void  logout () {
   system. out.println ( "system has been safely exited");
 }
 });
 }
} /span>

An anonymous inner class has the advantage of being able to define a class less, and its disadvantage is that it cannot be reused.

Search the public number "programmer Koala", welcome attention!

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.