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.


1. member Inner class

The 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 Internal classes use:

  1. Package com.dream;


  2. public class MyTest {


  3. public static void Main (string[] args) {


  4. Mother Mother=new Mother (12, "Xiao Ming");


  5. One of the methods of the instance inner class

  6. Mother.baby temp=mother.new Baby ();

  7. Temp.say ();


  8. Instantiating method two, accessing the inner class by invoking the member function of the class

  9. Mother.say ();


  10. }


  11. }


  12. Class mother{


  13. private int age;


  14. public String name;


  15. Public mother (int age, String name) {


  16. This.age = age;

  17. THIS.name = name;

  18. }


  19. Defining inner classes (member inner classes)

  20. Class baby{

  21. public void Say ()

  22. {

  23. System.out.println ("I have" +age+ "old", called "+name");

  24. }

  25. }


  26. Instantiating in a class

  27. public void Say ()

  28. {

  29. Baby Temp=new Baby ();

  30. Temp.say ();

  31. }


  32. }

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

As the name implies, the inner class defines the class within the method, which should be noted:

1. The inner class of the method 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 resides

  1. public class MyTest {

  2. public void Say (final String name)

  3. {//Method inner class

  4. Class Baby

  5. {

  6. public void Say ()

  7. {

  8. Final attribute required

  9. System.out.println ("name" +name);

  10. }


  11. }


  12. Instantiated inside the method

  13. Baby Temp=new Baby ();

  14. Temp.say ();

  15. }


  16. public static void Main (string[] args) {


  17. MyTest test=new MyTest ();

  18. Test.say ("Wang Xiao-shuai");


  19. }


  20. }

Copy Code

The scope of the inner class of a method 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.

  1. public class MyTest {


  2. public int age=1;


  3. public static String name= "small black";

  4. Defining static internal classes

  5. Static Class baby{


  6. public void Say ()

  7. {

  8. The Age property cannot be accessed, only static properties can be accessed

  9. System.out.println ("Name:" +name);

  10. }


  11. }


  12. public static void Main (string[] args) {

  13. Instantiate a static inner class

  14. Mytest.baby temp=new Mytest.baby ();

  15. Temp.say ();

  16. }


  17. }


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. So static inner classes can only access static properties of external classes


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

  1. public class MyTest {


  2. public static void Main (string[] args) {


  3. Quite inherit the mother class and implement the abstract method

  4. Mother Mother=new Mother (25, "Gui Lun mg") {

  5. public void Say ()

  6. {

  7. System.out.println ("Age:" +age+ "Name:" +name);

  8. }

  9. };

  10. Mother.say ();


  11. }


  12. }


  13. Abstract class mother{


  14. public int age;


  15. public String name;


  16. Public mother (int age, String name) {


  17. This.age = age;

  18. THIS.name = name;

  19. }


  20. public abstract void Say ();

  21. }

Copy Code

2. Interface anonymous inner class: Can not define class to implement the interface and implement the call of the interface

  1. public class MyTest {


  2. public static void Main (string[] args) {


  3. Equivalent to implementing an interface

  4. Baby Temp=new Baby () {

  5. @Override

  6. public void Say () {

  7. System.out.println ("Name:" +name);

  8. }

  9. };

  10. Temp.say ();


  11. }


  12. }


  13. Interface baby{


  14. String name= "Small black";//interface-defined properties are constants, decorated as public final static String name

  15. public void say ();

  16. }

Copy Code

3. Parametric anonymous inner class: Only the final property can be accessed

  1. public class MyTest {


  2. public void Say (Baby temp)

  3. {

  4. Temp.say ();

  5. }


  6. public static void Main (string[] args) {


  7. MyTest test=new MyTest ();


  8. final int age=10;

  9. Parameter anonymous inner class

  10. Test.say (New Baby () {


  11. @Override

  12. public void Say () {

  13. To access the properties of the final definition

  14. System.out.println ("Age:" +age);


  15. }

  16. });

  17. }


  18. }


  19. Interface baby{

  20. String name= "Small black";//interface-defined properties are constants, decorated as public final static String name

  21. public void say ();

  22. }

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

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.