Within Java class

Source: Internet
Author: User

(Static decorated members belong to the entire class, not to a single object)

Definition: Putting a class into the internal definition of another class, the internally defined class is called an inner class (and some nested classes), and the class containing the inner class is called the Outer class (also known as the Host Class).

1. Non-static inner class

Inner classes that do not use static adornments are non-static inner classes.

Note: A. Java does not allow static members to be defined in non-static inner classes

B. When you call an instance method in a non-static inner class, you must have a non-static member inner class instance, and a non-static inner class instance must be parasitic within the external instance

c. If the outer class member variable has the same name as the inner class member variable and the local variable of the method inside the inner class, it can be distinguished by this, the Outer class class name.

 Public classTest {PrivateString prop ="instance variables for external classes";  Public classinclass{PrivateString prop ="instance variables in an inner class";  Public voidinfo () {String prop="Local Variables"; System. out. println ("instance variable values for external classes:"+test. This. Prop); System. out. println ("instance variable value for inner class:"+ This. Prop); System. out. println ("Local Variables:"+prop); }    }     Public voidTest () {Inclass IC=NewInclass ();    Ic.info (); }     Public Static voidMain (string[] args) {/** Test ts = new Test ();         * Ts.test (); * The code works the same as above*/        Newtest (). Test (); }}

Use non-static inner classes:

Define the syntax format of the inner class outside the outer class: Outerclass.innerclass VarName

syntax to create a non-static inner class instance outside of the outer class: Outerinstace. New Innerconstructor ()

classout{classIn { PublicIn (String msg) {System.out.println (msg); }    }} Public classCreateinnerinstance { Public Static voidMain (string[] args) {//Outerclass.innerclass varName = new Outinstance.new innearconstructor ();Out.in in =NewOut ().NewIn ("Test information");        /*out.in in; *out out=Newout (); *in2= out.NewIn ("Test information");

*/
}}

2. Static Inner class

A static inner class is an inner class that is decorated with static, also known as a class inner class.

A. Static inner classes cannot access instance members of external classes, only class members of external classes (same as static adornments).

C. External classes still cannot access members in static inner classes, but you can use the class masterpieces of static inner classes to access class members in static inner classes, or you can use static inner class objects as callers to access instance variables in static inner classes.

 Public classStaticinnerclasstest {Private intProp = 5 ; Private Static intPROP1 = 6 ; Static classinclass{ Public voidAccessouterprop () {

private static int prop = 5;
private int prop1 = 6;
//System.out.println (prop); This code will go wrong, and the static inner class cannot access the non-static members of the outer class

System.out.println (PROP1); } }
public void Accessinnerpro () {
Class members in static inner classes are accessed through the class name of the static inner class


System.out.println (Inclass.prop);
To access an instance variable in a static inner class by using a static inner class object as the caller
SYstem.out.println (New Inclass (). Prop1);
}
}

Use static internal classes outside of external classes: (and similar to using non-static comparisons)
Syntax: New Outerclass.innerconstructor ()

class out{    staticclass  in    {        public in  () {            System.out.println ("Constructor for static inner class");}     }}  Public class createstaticinnerinstance {    publicstaticvoid  main (string[] args) {        new  out.in ();    }}

3. Anonymous inner class
If an inner class is defined in a method, the inner class is a local inner class, and the local inner class is only valid in that method.

To define the format of an anonymous inner class:

new Implementation Interface | Parent class Constructor (argument list) {  class body part}

As you can see, an anonymous inner class must inherit a parent class or implement an interface.

Interfaceproduct{ PublicDouble GetPrice ();  PublicString getName ();} Public classAnonymtest { Public voidCE (Product p) {System.out.println ("Purchased a" + p.getname () + ", spent out" +P.getprice ()); }     Public Static voidMain (string[] args) {anonymtest at=Newanonymtest (); At.ce (NewProduct () {@Override PublicDouble GetPrice () {return0.5; } @Override PublicString GetName () {return"Spicy Bar";    }        }); }}

Within Java class

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.