Java Learning Note within class

Source: Internet
Author: User

1. What is the inner class in Java

An inner class (Inner Class) is a class that is defined in another class. Corresponding to this, the class containing the inner class is called the outer class.

Its main role:

A. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class.

B. Methods of the inner class can access all the data of the external class directly, including private data.

C. Functions implemented by internal classes can also be implemented using external classes. It is sometimes more convenient to use internal classes.

D. Internal classes can be divided into: Member inner class, Static inner class, method inner class, anonymous inner class four kinds.

Simple example:

//External class HelloWorld Public classHelloWorld {//Inner class inner, class inner inside class HelloWorld         Public classInner {//methods for inner classes                 Public voidShow () {System.out.println ("Welcome to Imooc!"); }        }             Public Static voidMain (string[] args) {//creating an external class objectHelloWorld Hello=NewHelloWorld (); //creating an inner class objectInner i= Hello.NewInner (); //methods for invoking inner class objectsi.show (); }}

2. member Inner classes in Java

The most common inner class is the member inner class, which is also a normal inner class. Such as:

//external Outer Public classouter{Private inta=9;//private properties of external classes//Inner class inner     Public classinner{intb = 2;//member properties for inner classes         Public voidTest () {System.out.println ("Access A in an external class:" +a); System.out.println ("Access B in inner class:" +b); }    }    //Testing Internal Classes     Public Static voidMain (string[] args) {Outer o=NewOuter ();//creates an external class object with the object name 0;Inner i = O.NewInner ();//Create an inner class object with an external class object named II.test ();//invokes the test method of the inner class object.     }}

As you can see from the code above. How to use a member's inner class:

A. The inner class is defined inside the outer class and is equivalent to the position of a member variable of the outer class, and the inner class can use any access control, such as Public/protected/private.

B. The test () method defined in the inner class can directly access the data in the outer class without being affected by the access control. such as direct access to private attribute a in the outer class

C. After defining a member's inner class, use an external class object to create an inner class object, rather than go directly to the new inner class object, i.e. the inner class object name = Outer class object. New inner class ();

D. After compiling the above program, it is found that two. class files have been generated. One is the internal classes. class file. The other one is the outer class $ inner class file.

It is important to note that:

A. External classes cannot directly use members and methods of inner classes

B. If the outer class and the inner class have the same member variable or method, the inner class accesses its own member variable or method by default, and if you want to access the member variables of the external class, you can use the external class. This member variable.

3. Static inner classes in Java

Static inner classes are internal classes of static adornments, which are characterized by:

A. Static inner classes cannot directly access non-static members of an external class, but can be accessed by means of the new external class () member.

B. If the static member of the outer class is the same as the static member name of the inner class, the static member of the external class can be accessed through the class name. static member; If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name.

C. When you create an object of a static inner class, you do not need an object of an external class, you can directly create an inner class object name = New inner class ();

//External Class Public classsouter{Private intA = 99;//private variables for external classes    Static intb = 1;//static variables for external classes//static inner class     Public Static classsinner{intb=2;//variables for inner classes         Public voidTest () {System.out.println ("Access B in the external class:" +souter.b); System.out.println ("Access B in inner class:" +b); }    }    //testing static Internal classes     Public Static voidMain (string[] args) {Sinner Si=NewSinner ();//Create an inner class object directlySi.test ();//Call the test method    }}

Method Inner class in 4.Java

The inner class of the method is the inner class defined in the method of the outer class, and the inner class of the method is visible only inside the method. That is used only within the method.

//External Class Public classmouter{//methods in the outer class     Public voidShow () {Final intA = 25;//Constants        intb = 13;//variables//method Inner Class        classinner{intc=2;//variables in the inner class             Public voidprint () {System.out.println ("Constant A in methods of accessing an external class:" +a); System.out.println ("Access variable C in inner class:" +c); }} minner mi=NewMinner ();.    Create method Inner Class object Mi.print (); }    //test method Inner class     Public Static voidMain (string[] args) {mouter mo=NewMouter ();//create an object of an external classMo.show ();//calling the Show method of an external class    }}

Java Learning Note within class

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.