Java Internal classes

Source: Internet
Author: User
Tags modifiers

 PackageInnerclass;//In Java, a class can be defined inside another class or inside a method, and such a class is called an inner class. The broad sense of internal classes generally includes these four types: member inner class, local inner class, anonymous inner class, and static inner class Public classOutter {/*private can decorate data members, construct methods, method members, and not decorate classes (here refers to external classes, regardless of inner classes) protected can decorate data members, construct methods, method members, and cannot decorate classes (this refers to external classes, regardless of inner classes). An inner class (static/member) can be seen as a member of an external class so that it can have private access, protected access, public access, and default (package access permissions) The local inner class is like a local variable inside a method. Cannot have public, protected, private, and static modifiers.*/    Static intA=1; intB=5; Static voidT1 () {System.out.println ("Static methods for external classes"); }    voidT2 () {System.out.println ("Non-static methods for external classes"); }    /*1. Static inner class: As a static member of a class, exists inside a class. A static inner class is a member of an external class, but an object of a static inner class can be created directly without creating an object of the outer class. Static inner classes can refer to static member variables and static methods of external classes, but cannot refer to ordinary members of external classes. */    Static classinner1{ Public voidtest1 () {System.out.println ("Direct access to static member variables of an external class: The value of A is" +a ";//directly referencing static member variables of an external classT1 ();//static methods that directly refer to external classes//System.out.println (the value of "B" is "+b");//an attempt was made to reference a non-static member variable of an external class, not by compiling//T2 ();//an attempt to reference a non-static method of an external class cannot be compiled bySYSTEM.OUT.PRINTLN ("cannot be accessed directly, you can only access non-static members of an external class by means of the new external class ()." The value of B is "+NewOutter (). b);//static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external class (). Member          Newoutter (). T2 (); System.out.println ("Method ~~~~~~~~~~~~~~~ for static internal classes"); }    }    /*2. member Inner class: As a member of a class, exists inside a class. A member inner class can invoke all the properties and member methods of an external class, including private and static members, but only after an object of the outer class has been created can the object of the member's inner class be created. Therefore, the member inner class cannot contain static methods and static member variables, because the static variable is to occupy memory, at compile time as long as is defined as static variable, the system will automatically allocate memory to him, this also is the member inner class is the external class compiles compiles, that is, must have the external class exists to have the inner class, because the system Execute: Run external class---static variable memory allocation---inner class, where static variables are generated before internal classes, which is obviously not possible, so you cannot define static variables! */    classinner2{ Public voidtest2 () {intA = 20; intB=28; System.out.println ("To access a member of the same name for an external class, you need to pass the external class (). The method of the static member variable: The value of A is" +outter. This. a);//accessing static member variables for external classesSystem.out.println ("A hidden behavior is accessed by a static member variable of a member's inner class: The value of A is" +a);//a member of a member's inner class is accessed by a hidden occurrenceT1 ();//to invoke a static method of an external classSystem.out.println ("To access members of the same name for an external class, you need to pass the external class ()." This is a non-static member variable in the way: B has the value "+outter." This. b);//accessing non-static member variables of an external classSYSTEM.OUT.PRINTLN ("A non-static member variable of a member's inner class is accessed by a hidden phenomenon: the value of B is" +b);//a member of a member's inner class is accessed by a hidden occurrenceT2 ();//calling a non-static method of an external classOutter. This. T2 (); System.out.println ("Method ~~~~~~~~~~~~~~~ for member inner class"); }       /*When a member inner class has a member variable or method with the same name as an external class, a hidden phenomenon occurs, that is, by default, members of the members inner class are accessed. If you want to access a member of the same name as an external class, you need access: External class (). This member*/              voidT2 () {System.out.println ("Non-static method for member inner class"); }    }        /*3. Local inner class: exists in the interior of a method.      The local inner class can only be used inside the method, and once the method executes, the local inner class is removed from memory. It is important to note that if a local variable in his method is to be used in a local inner class, then the local variable needs to be defined as final. It differs from the member inner class in that the access of the local inner class is limited to the method or the scope*/     Public voidTest () {Final intC=5; System.out.println ("In Methods of external classes"); classinner3{voidtest3 () {System.out.println ("Direct access to the value of the static member variable A of the external class is" +a ";//accessing static member variables for external classesT1 ();//to invoke a static method of an external classSYSTEM.OUT.PRINTLN ("Direct access to the value of the non-static member variable B of the external class is" +b);//accessing non-static member variables of an external classT2 ();//calling a non-static method of an external classSystem.out.println ("accesses a local variable in his method (must be final decorated): The value of C is" +c);//Note: If you want to use a local variable in his method in a local inner class, you need to define the local variable as final (the scope of the local variable is expanded when local variables are accessed by the internal class of the authority). System.out.println ("Method ~~~~~~~~~~~~~~~ for local inner classes"); }} Inner3 Inner3=NewInner3 (); Inner3.test3 ();//local inner classes can only be used inside a method    }         Public voidout () {//In an external class, if you want to access the members of an inner class, you must first create an object of the member's inner class, and then use a reference to that object to visit:        NewInner2 (). Test2 (); NewInner1 (). Test1 (); }         Public Static voidMain (string[] args) {Inner1 in1=NewInner1 ();//objects of a static inner class can be created directly without first creating an object of the outer classIn1.test1 (); //Inner2 in=new Inner2 (); objects within a member's inner class cannot be created directly and will be error-Outter out1=NewOutter ();//create an object of the outer class firstInner2 IN2=OUT1.NewInner2 ();//NOTE:!! An object of a member's inner class must be created from an object of an external classIn2.test2 ();            Out1.test (); /*4. Anonymous inner class: A class that exists inside a class but has no class name. 1) An anonymous inner class cannot be an abstract class, because an object of an inner class is created immediately when the system creates an anonymous inner class.      Anonymous inner classes are therefore not allowed to be defined as abstract classes. 2) Anonymous inner classes are unequal to the definition constructor (the constructor method, the only class that has a constructor), because the anonymous inner class does not have a class name, so the constructor cannot be defined, but an anonymous inner class can define an instance initialization block. 3) Anonymous Inner classes also cannot have access modifiers and static modifiers. */outter Out2=NewOutter () { Public voidTest () {System.out.println ("Method test for anonymous inner class");     }      }; /*The say method does not exist in the Outter method and can only be called by the following method to invoke the custom method of the anonymous inner class say () object of the non-anonymous inner class. Anonymous inner class does not have the name of the inner class, cannot use the instance object to invoke its methods, but the method of inheriting the parent class and the method of implementation can be normally called, in this example, the anonymous inner class inherits the class Outter (abstract class and interface can) the test method, so can be called by the Outter object. */      NewOutter () { Public voidTest () {System.out.println ("Method test for anonymous inner class"); }           Public voidsay () {System.out.println ("Method say for anonymous inner class"); }}.say ();//in fact, anonymous inner class implicitly an anonymous object, through which the say () and test () method can be called directly;out2.test (); }}

The operation results are as follows

New external Class (). Methods for accessing non-static members of external classes: The value of B is 5 method of the static inner class of a non-static method of the outer class ~~~~~~~~~~~~~~~ to access a member of the same name as an external class, you need to pass an external class ().  This . Static member variables: A with a value of 1 is a static member variable that is accessed by a member inner class that has a value of 20: A static method of the outer class to access a member of the same name as the external class, requires an external class ().  This . Non-static member variables: A value of B of 5 occurs when a hidden behavior is accessed by a non-static member variable of a member inner class: The value of B is a non-static method of a 28 member inner class method of a non-static method member inner class of the outer class ~~~~~~~~~~~~~~~  In the method of an external class, access the static member variable A of the external class directly to the value of 1 the static method of the outer class directly accesses the value of the non-static member variable B of the outer class to 5 the non-static method of the outer class accesses the local variable in his method (must be decorated with a final): C ~ ~ ~~~~~~~~~~~~~ methods of anonymous inner classes say methods of anonymous inner classes test

Refer to the blog link https://www.cnblogs.com/dolphin0520/p/3811445.html

Https://www.cnblogs.com/hapjin/p/5744478.html

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.