Java Internal classes

Source: Internet
Author: User

In Java, a class can be defined inside another class or inside a method, and such a class is called an inner class. The inner class is still a separate class, and after compilation The inner class is compiled into a separate. class file, but preceded by the class name and the $ symbol of the outer class. An inner class can indirectly resolve multiple inheritance problems by inheriting a class using an inner class, which inherits a class and implements multiple inheritance.
Inner classes are mainly divided into members inner classes, method inner classes, static inner classes, anonymous inner classes.

The

1. member Inner class
member inner class is, by definition, a member property of an external class, except that the property is not a string,int type, but a class type. The class's member inner class can access the properties and methods of the class, because he is also a member of the class.
member Inner class use:
Package com.dream;

public class MyTest {

public static void Main (string[] args) {

Mother mother=new mother (12, " Xiao Ming ");

//One of the methods of the instance inner class
Mother.baby temp=mother.new Baby ();
Temp.say ();

//Instantiate method two, accessing the inner class
Mother.say () by invoking the member function of the class;

}

}

class mother{

private int age;

Public String name;

Public mother (Int., String name) {

This.age = age;
THIS.name = name;
}

//define inner Class (member inner Class)
class baby{
public void Say ()
{
System.out.println ("I Already" +age+ "years old, name" + name);
}
}

//Instantiate in class
public void Say ()
{
Baby temp=new baby ();
Temp.say ();
}

}
Copy code the
member inner class is a member of the class, and the members of the class depend on the class object, so the inner class is also dependent on the object, and the class needs to be instantiated for use, so an external class object is used to instantiate the inner class.

2. Method Inner class
The name of the method inner class defines the class within the method, it should be noted that:
1. The method inner class can only be instantiated in the method that defines the inner class, and it cannot be instantiated outside this method
2. Method Inner Class object cannot use the non-final property of the method in which the inner class is located
public class MyTest {
public void say (final String name)
{//Method inner class
class Ba By
{
public void say ()
{
//requires final attribute
System.out.println ("name" +name);
}

}

//Instantiate inside the method
Baby temp=new baby ();
Temp.say ();
}

public static void Main (string[] args) {

MyTest test=new MyTest ();
Test.say ("Wang Xiao-shuai");

}

}
Copy Code
Method The scope of the inner class is limited to that method, so it is instantiated within the method. The variable defined in the method is a local variable, leaving the method, the variable loses its function, it is automatically eliminated, and the inner class does not leave its method to lose its function, it has a broader life cycle, so it needs to be copied into the inner class, and the copy will bring inconsistencies, which requires the use of Final declaration to ensure consistency.

3. Static Inner class
That is, define a static inner class in a class, static is that the inner class can be like other static members, no instance of the external class object can also access the call, static property does not belong to the instance object of the class.
public class MyTest {

public int age=1;

public static String name= "small black";
Defining static internal classes
Static Class baby{

public void Say ()
{
The Age property cannot be accessed, only static properties can be accessed
System.out.println ("Name:" +name);
}

}

public static void Main (string[] args) {
Instantiate a static inner class
Mytest.baby temp=new Mytest.baby ();
Temp.say ();
}

}

Copy Code
It is important to note that static inner classes can only access static member properties and methods in external classes, because static properties can be accessed without using external instance objects, and if the property is not a static property, you need to rely on the instance object of the outer class. Therefore, the static inner class can only access static properties of the outer class. Usually need to learn more read Java Video tutorial Ah

4. Anonymous inner class
Anonymous inner class is a bit like anonymous object, no name, can only be used once, anonymous inner class is divided into three kinds of cases
1. Inherited anonymous inner class: You can not define class to inherit the abstract class and implement an instance of the abstract class and run
public class MyTest {

public static void Main (string[] args) {

Quite inherit the mother class and implement the abstract method
Mother Mother=new Mother (25, "Gui Lun mg") {
public void Say ()
{
System.out.println ("Age:" +age+ "Name:" +name);
}
};
Mother.say ();

}

}

Abstract class mother{

public int age;

public String name;

Public mother (int age, String name) {

This.age = age;
THIS.name = name;
}

public abstract void Say ();
}
Copy Code
2. Interface anonymous inner class: Can not define class to implement the interface and implement the call of the interface
public class MyTest {

public static void Main (string[] args) {

Equivalent to implementing an interface
Baby Temp=new Baby () {
@Override
public void Say () {
System.out.println ("Name:" +name);
}
};
Temp.say ();

}

}

Interface baby{

String name= "Small black";//interface-defined properties are constants, decorated as public final static String name
public void say ();
}
Copy Code
3. Parametric anonymous inner class: Only the final property can be accessed
public class MyTest {

public void Say (Baby temp)
{
Temp.say ();
}

public static void Main (string[] args) {

MyTest test=new MyTest ();

final int age=10;
Parameter anonymous inner class
Test.say (New Baby () {

@Override
public void Say () {
To access the properties of the final definition
System.out.println ("Age:" +age);

}
});
}

}

Interface baby{
String name= "Small black";//interface-defined properties are constants, decorated as public final static String name
public void say ();
}
Copy Code
Explains why you use the final property
It is mainly the inconsistency between the life cycle of the local variable and the object of the local inner class.

Internal class inside the use of the external class local variables, is actually the inner class object in use it, the internal class object life cycle can call it, while the inner class trying to access local variables in the external method, the local variables of the external method is probably no longer exist, it is necessary to continue their lives, copied into the inner class, Copies, in turn, create inconsistencies that require the use of final statements to ensure consistency. Replication guarantees a life-cycle continuation, and final guarantees a consistent reference.

Java Internal classes

Related Article

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.