In this chapter we discuss nested classes.
1. Concept
Inner classes are nested classes when static is used.
Package Com.ray.ch07;public class Test {private static Class mytest3{}}
MYTEST3 is a nested class
2. Attention points
(1) can only communicate with the static part of the outer class
(2) Do not hold references to external class objects, you can use the static property directly
Package Com.ray.ch07;public class Test {private int id = 1;private static String name = "AAA";p rivate Static class MyTest1 {public void Say () {//System.out.println (ID);//errorsystem.out.println (name);}} Private class MyTest2 {public void Say () {System.out.println (ID); SYSTEM.OUT.PRINTLN (name);}} public static void Main (string[] args) {//New test (). New MyTest1 ();//error.new test.mytest1 (). Say (); new test (). New Mytes T2 ();}}
From the above code can be seen, MyTest1 is not able to access the ID, and MYTEST2 can.
Also, at new, we can see that the MyTest1 has the static characteristics, does not need new, but still need to add a parenthesis, and MyTest2 need new to invoke the method inside.
(3) Nested classes do not have this reference
Because nested classes use static, you cannot use this to refer to external classes.
Summary: This chapter discusses the concept of nested classes and the points of attention.
This chapter is here, thank you.
-----------------------------------
Directory
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding java-8.9 Nested classes from the beginning