In Java, it is permissible to define another class within one class (or method, block of statements), called an inner class (Inner Class), sometimes called a nested class (Nested).
There is a logical dependency between the inner class and the outer wrapper class, which is typically used only within the class or statement block that defines it, to implement some functional logic that has no general meaning, and must give the full name when it is referenced externally.
The main reasons for using inner classes are:
- An inner class can access data in an external class, including private data.
- Inner classes can be hidden from other classes in the same package.
- Using an anonymous (anonymous) inner class is convenient when you want to define a callback function and do not want to write a lot of code.
- Reduce naming conflicts for classes.
Take a look at the following example:
1 Public classOuter {2 Private intsize;3 Public classInner {4 Private intCounter = 10;5 Public voidDostuff () {6size++;7 }8 }9 Public Static voidMain (String args[]) {TenOuter Outer =NewOuter (); OneInner Inner = outer.NewInner (); A Inner.dostuff (); - System.out.println (outer.size); - System.out.println (inner.counter); the //compilation error, external class cannot access variables of inner class - System.out.println (counter); - } -}
This code defines an external class Outer, which contains an inner class Inner. Comment out the error statement, compile, generate two. class files: Outer.class and Outer$inner.class. In other words, the inner class is compiled into a separate byte-code file.
An inner class is a compiler phenomenon that is independent of the virtual machine. The compiler will translate the inner class into a regular class file that separates the external class name from the internal class name with the $ symbol, and the virtual machine knows nothing about it.
Note: An object of an outer class must be preceded to generate an object of the inner class, because the inner class needs to access the member variables in the outer class, and the member variables must be instantiated to make sense.
The inner class is a new feature of Java 1.1, and some programmers consider it a commendable improvement, but the syntax of the inner class is complex, damaging the good code structure, and violating Java's simpler design concept than C + +.
The inner classes seem to have added--something beautiful and interesting, which is not necessary, does it also allow Java to embark on the destructive path of many languages? This tutorial is not intended to give a definite answer to this question.
Series Articles:
Java know how much (1) Language overview
Java know how much (2) virtual machine (JVM) and cross-platform principle
Java know how much (3) employment direction
Java know how much (4) the difference between J2SE, Java EE, J2ME
Java know how much (5) Build Java development environment
Java know how much (6) The first example of a program
Java knows how many (7) classes and objects
Java know how much (8) class library and its organizational structure
Java know how much (9) Import and Java class search path
Java know how much (10) data types and variables
Java know how much (11) data type conversions
Java know how many (12) operators
Java know how much (13) Process Control
Java know how many (14) arrays
Java know how much (15) string
Java know how much (StringBuffer) and Stringbuider
Java know how much (17) emphasize the programming style
Java know how many (18) classes are defined and instantiated
Java know how many (19) access modifiers (access control characters)
Java knows how many (20) variables are scoped
Java know how much (+) This keyword is detailed
Java know how many (22) method overloads
Java know how much (23) the basic run order of classes
Java know how much (24) packaging class, unpacking and packing detailed
Java know how much (25) More about Java package
Java know how much (26) claim rules for source files
Java know how much (27) the concept and implementation of inheritance
Java know how many super keywords
Java know how much (29) Overwrite and reload
Java know how much (30) polymorphic and dynamic binding
Java know how many static keywords and Java static variables and static methods
Java know how much (instanceof)
Java know how much (33) type conversions for polymorphic objects
Java know how much (final keyword): Block inheritance and polymorphism
Java knows how many () object classes
Java know how much (36) inner class and its instantiation
Java know how much (36) inner class and its instantiation