Java Internal classes

Source: Internet
Author: User

Inner class (innerclasses)

In general, we define classes as separate Units. In some cases, we put a class in the internal definition of another class, called an inner class.

The function of the inner class:

1. The inner class provides a better Package. Only external classes can be accessed directly, and other classes in the same package are not allowed to be accessed directly.

2. The inner class can access the private properties of the outer class directly, and the inner class is treated as a member of its outer class. External classes, however, cannot access internal properties of the inner class.

The use of internal classes:

Because the inner class provides better encapsulation characteristics, it is convenient to access the properties of the external class. therefore, internal classes are typically used in cases where services are provided only for the external class in which they are located.

Classification of inner Classes:

1. member inner class (can be decorated arbitrarily with private, proteted, public). Class File: external class $ inner class. Class)

A) non-static inner class (the outer class uses non-static inner classes and usually uses no other Class)

I. A non-static inner class must be sent to an external class Object. therefore, If there is a non-static inner class object Then there must be a corresponding outer class Object. A non-static inner class object belongs to an object of an external class individually.

Ii. non-static Inner Classes can use members of external classes, but external classes cannot directly access non-static inner class Members.

Iii. non-static Inner classes cannot have static methods, static properties, static initialization blocks.

Iv. static members cannot access non-static members: static methods for external classes, static code blocks cannot access non-static inner classes, including the inability to define variables with non-static inner classes, and create Instances.

V. Member variable access points:

1. Local variables of methods inside classes: variable names

2. Intrinsic class properties: This. variable name

3. External class Properties: external class name. This. variable name

Vi. access to internal classes:

1. Inner class defined in external class: new Innerclass ()

2. Use non-static inner classes outside the outer class:
Outer.Inner varname = outerobject.new inner ()

/*** Internal class *@authorOU **/ public classinnerclasses { public Static voidmain (string[] Args) {/*face face=new face ();    Face.nose nose=face.new Nose ();    Nose.breath (); */face.ear Ear=NewFace.ear ();      Ear.listen (); }    }classface{inttype; String face= "oval face"; /*** Non-static Inner class for example *@authorOU **/    classnose{inttype; voidBreath () {//the external type is calledSystem.out.println (face. this. type); //if the inner class has no properties of the outer class, you can call directlySystem.out.println (face); }    }        /*** Static inner class for example*/Static classear{voidListen () {System.out.println ("i'm listening."); }}        }
innerclasses

B) static internal class

I. Define the Way:

Static Class ClassName {

Class Body

}

Ii. Key points of use:

1. When a static inner class object exists, it does not necessarily exist for the corresponding outer class Object. therefore, An instance method of a static inner class cannot directly access an instance method of an external class.

2. A static inner class is considered a static member of an external class. therefore, the method of the outer class can be passed: static inner class. The name accesses static members of the static inner class. An instance of a static inner class is accessed through the new static inner class ().

3. Create a static inner class outside the outer class:

Face.teststaticinner Ainner = new Face.teststaticinner ();

2. anonymous inner class

Suitable for classes that only need to be used Once. For Example: keyboard monitoring operation and so On. Grammar:

The new parent class constructor (the argument class Table) implements the interface () {

Anonymous inner class class body!

}

  

Abstract classperson { public Abstract voidEat ();}  public classDemo { public Static voidmain (string[] Args) {person p=Newperson () { public voideat () {System.out.println ("eat something");        }        };    P.eat (); }} run result: eat something
person Anonymous Inner class

Local inner class

Defined inside a method. Scopes are limited to this method. Use very little.

Java Internal classes

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.