Learn java--inner class from the beginning

Source: Internet
Author: User



I. Inner class inner classes are classes defined within a class whose definition is directly contained in another class. An inner class can be considered to be a member of an external class, with the same status as other members of the class. Why use internal classes? We know that Java is completely object-oriented, encapsulating everything with objects, when a class contains many methods and fields, many method fields are logically related, so you can put them together and encapsulate them with an inner class. That is, when we describe a thing, there is one thing in it, and the inner thing is described by the inner class. After the inner class is compiled, the inner class will also produce a. class file, named External class name $ internal class name. class
The outer class outer{private int value = 3;//Inner class inner{//inner class method that directly accesses members of the outer class, and private members can also access void function () { System.out.println ("I can get Outer value:" + value);}} Methods of the outer class, to access the methods or members of the inner class, need to establish an inner class of Object void Function2 () {//To establish an inner class object Inner in = new Inner (); In.function ();}} public class Innertest {public static void main (string[] args) {//Create external class object, call method Outer out = new Outer (); Out.function2 ();//Hui Sis Create an inner class object, but infrequently, the general inner class is privatized and does not create an object Outer.Inner in = new Outer (). New Inner ();}


This is a member of the inner class, that is, the inner class is defined in the class without static adornments. Key points: 1. A member inner class that can access any member in an external class. External classes can also access members and fields of inner classes, but you need to create an inner class object beforehand. 2. Use the scene of a member's inner class: When a class has more functionality to complete, and some of the method fields function Similarly, it can be encapsulated into an inner class. 3. In general, the member inner class is set to private and does not allow direct access to the outside world.
two. Static Inner class
When a class is in a member position and there is a static modification, we call it a static inner class.
External class class outer1{//external class static member private static int max_value = 100;//External class static method public static void Printmaxvalue () { System.out.println (max_value);} Static inner class static inner{//static member of the inner class, having a crystal member, the inner class must be declared as static public static void Printlnoutervalue () {System.out.println ( Max_value);}}} public class InnerTest2 {public static void main (string[] args) {///static method, directly called by the class name Method Outer1.Inner.printlnOuterValue ();}}


Key points: 1. When a static member is defined in an inner class, the inner class must be declared as a static inner class. When a static method of an external class accesses an inner class, the inner class must also be static. 2. Static inner classes can only access static fields or methods of external classes. 3. Methods that invoke static internal classes are generally called directly through the external class name. The internal class name. Method name.

Three. local inner class native inner class is a class that is built into a method and is not much different from the inner class of the member, and is not used much to create the object.
public class Localinnerclasstest {public static void main (string[] args) {testlocalinnerclass ();} A static method that uses the local inner class private static void Testlocalinnerclass () {//is defined inside the inner class of the method body, notice the semicolon class after the closing parenthesis mylocalinnerclass{ private int field=100;private void Printvalue () {System.out.println (field);}};   The local inner class is also instantiated before it can be used Mylocalinnerclass localobj=new mylocalinnerclass (); Localobj.printvalue ();}}

Four. Anonymous inner class anonymous inner class is actually a simplified form of inner class. He does not name a good thing, so he has no names!
Example:
Abstract class Innerbase{public abstract void function ();} Class outer2{//creates an inner class that inherits from the parent class. But it's too cumbersome to write, so here's a simplified notation, the anonymous inner class//class Inner extends Innerbase//{//public void function ()//{//system.out.println ("Inner Class ");//}//}//anonymous internal class public void method () {//Creates a parent class object, and overwrite the internal function method, and calls the function methods through the object new Innerbase () {public void function () {System.out.println ("Anonymous inner Class!");}}. function ();}} public class InnerTest3 {public static void main (string[] args) {Outer2 out = new Outer2 (); Out.method ();}}



Key points: 1. Anonymous internal classes are used only if the class must inherit a class or implement an interface. 2. Anonymous inner class definition format: New parent class (interface) {Overwrite method}. method. 3. The essence of an anonymous inner class is an inner class object that is implemented and invoked together. Simplified writing, but not very well understood. 4. Anonymous inner classes are characterized by just one use.



Learn java--inner class from the beginning

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.