Deep understanding of java Nested classes and internal classes, and deep understanding of java Nested classes

Source: Internet
Author: User

Deep understanding of java Nested classes and internal classes, and deep understanding of java Nested classes

1. What are nested classes and internal classes?

You can define another class within a class. This class is called the nested classes. It has two types: static nested class and non-static nested class. Static Nested classes are rarely used. The most important is non-static Nested classes, that is, they are called internal classes (inner ). Nested classes are introduced from JDK1.1. The inner class can be divided into three types:
1. Internal classes directly defined in a class (external class;
2. Internal classes defined in a method (external class method;
Third, anonymous internal class.

The following describes the usage and precautions of these Nested classes.

Ii. Static nesting

The following code defines a static nested class,
  1. Public class StaticTest {
  2. Private static String name = "javaJohn ";
  3. Private String id = "X001 ";
  4. Static class Person {
  5. Private String address = "swjtu, chenDu, China ";
  6. Public String mail = "josserchai@yahoo.com"; // internal class public Member
  7. Public void display (){
  8. // System. out. println (id); // you cannot directly access non-static members of an external class.
  9. System. out. println (name); // only the static members of the external class can be accessed directly.
  10. System. out. println ("Inner" + address); // access the internal class member.
  11. }
  12. }
  13. Public void printInfo (){
  14. Person person = new Person ();
  15. Person. display ();
  16. // System. out. println (mail); // inaccessible
  17. // System. out. println (address); // inaccessible
  18. System. out. println (person. address); // you can access private members of an internal class.
  19. System. out. println (person. mail); // you can access public members of the internal class.
  20. }
  21. Public static void main (String [] args ){
  22. StaticTest staticTest = new StaticTest ();
  23. StaticTest. printInfo ();
  24. }
  25. }


In a static nested class, you cannot access non-static members of the external class, which is limited by "static methods cannot access non-static members directly" in Java syntax. To access external class variables, you must use other methods. For this reason, static Nested classes are rarely used. Note: The members of the external class access internal class are special and cannot be accessed directly, but they can be accessed through the internal class. This is because all the Members and methods in the static nesting are considered static. Note that the internal static class Person is only visible within the StaticTest class. If it is referenced or initialized in other classes, it is incorrect.

3. Define internal classes in external classes

The following code defines two internal classes and their call relationships for external classes:
  
  1. Public class Outer {
  2. Int outer_x = 100;
  3. Class Inner {
  4. Public int y = 10;
  5. Private int z = 9;
  6. Int m = 5;
  7. Public void display (){
  8. System. out. println ("display outer_x:" + outer_x );
  9. }
  10. Private void display2 (){
  11. System. out. println ("display outer_x:" + outer_x );
  12. }
  13. }
  14. Void test (){
  15. Inner inner = new Inner ();
  16. Inner. display ();
  17. Inner. display2 ();
  18. // System. out. println ("Inner y:" + y); // internal variables cannot be accessed.
  19. System. out. println ("Inner y:" + inner. y); // you can access
  20. System. out. println ("Inner z:" + inner. z); // accessible
  21. System. out. println ("Inner m:" + inner. m); // accessible
  22. InnerTwo innerTwo = new InnerTwo ();
  23. InnerTwo. show ();
  24. }
  25. Class InnerTwo {
  26. Inner innerx = new Inner ();
  27. Public void show (){
  28. // System. out. println (y); // The Innter's y member cannot be accessed.
  29. // System. out. println (Inner. y); // you cannot directly access any member or method of Inner.
  30. Innerx. display (); // accessible
  31. Innerx. display2 (); // accessible
  32. System. out. println (innerx. y); // accessible
  33. System. out. println (innerx. z); // accessible
  34. System. out. println (innerx. m); // accessible
  35. }
  36. }
  37. Public static void main (String args []) {
  38. Outer outer = new Outer ();
  39. Outer. test ();
  40. }
  41. }


The above code must be noted that for internal classes, public or private delimiters are usually not added before the class Keywords of the class are defined. If the class keyword is added, at the same time, it seems that these qualifiers have no influence on the internal class variables and methods (?). In addition, it should be noted that the Inner and InnterTwo of the internal class are only known within the scope of the class Outer. If any code outside the class Outer tries to initialize the class Inner or use it, compilation fails. At the same time, the variable members of the internal class can only be seen inside the internal. If the external class or the internal class at the same level needs to be accessed, the method in the sample program should be used, and the variables of the internal class cannot be directly accessed.
Applications of JAVA internal and Nested classes

Class {
Class Ainner {
Class Inner {

}
}
}
Do you mean this?

JAVA internal class nesting

First of all, the dynamic nesting of ABC is certainly suspected of serious misuse ..
If you want to forcibly reference A member in an instance object, write it as A. this. methodOfA ();
That is
Public void fun (){
System. out. println (A. this. propertyX );
A. this. methodOfA ();
}
If the inner class does not change with the content of the Instance Object of the outer class, the inner class must be added with static class C as the static class. This reduces unnecessary resource waste by an order of magnitude.
If the inner class is public, it is called by other classes, or the logical subordination is not obvious. consider not writing it into the inner class. Write a java file separately.

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.