Four internal classes of Java

Source: Internet
Author: User

/** * General Inner class: The normal inner class is not static decorated and is defined in the Outer class class body. * 1. Methods in the general inner class can directly use instance variables and instance methods of the outer class. * 2. In a regular inner class you can create an object directly from an inner class */public class Myouter {private int x = 100;class myinner{private String y= "hello!"; public void Innermethod () {System.out.println ("String =" in inner class +y); System.out.println ("x =" +x in the outer Class); Outermethod (); System.out.println ("x is" +myouter.this.x);}} public void Outermethod () {x + +;} public void Makeinner () {//creates an inner class instance in an external class method Myinner in = new Myinner ();} public static void Main (string[] args) {myouter mo = new Myouter ();//using an external class construction method to create an MO regular inner class requires an external class instance to Newmyouter.myinner Inne r = Mo.new Myinner (); Inner.innermethod ();}} /** * Static inner class: Similar to other members of the class, you can decorate the inner class with static, which is called a static inner class. Static inner classes are similar to static internal methods, * Access only static members of external classes, * Cannot directly access instance variables of external classes, and instance methods, only accessible through object references. */public class MyOuter2 {public static int x=100;public static class myinner{private String y= "hello!"; * * Because the static inner class does not have any references to the external class instance, * The This keyword cannot be used in the static inner class to access instance members in external classes, but can access static members in external classes. This communicates with the static method of the general class */public void Innermethod () {System.out.println ("x=" +x); SystEm.out.println ("y=" +y);} public static void Main (string[] args) {Myouter2.myinner si = new Myouter2.myinner (); Si.innermethod ();}} /** * Local Inner class: A class that is defined inside a method body or statement block (including methods, construction methods, local blocks, or static initialization blocks) becomes a local inner class. * The local inner class cannot add any access modifiers because it is only valid for local blocks. * 1. The local inner class is only valid in the method body and, like the local variable you want to define, you cannot create an object of the local inner class outside the defined method body * 2. When defining a class within a method, you should be aware of the following issues: * 1. Method defines local internal similarity method defines local variable, cannot use private, Protected, public, etc. access modifier specifier adornments, * Also cannot use static adornments, but can use final and abstract adornments * 2. The inner class in the method can access the external class member. For parameters and local variables of a method, you must have a final decoration to access it. * The inner class defined in the 3.static method can access the static member of the outer class definition * */public class MyOuter3 {private int size=5,y=7;public Object makeinner (int local Var) {final int finallocalvar = localvar;//creates an inner class that works only in the Makeinner () method, just like a local variable. The object of the Myinner class cannot be created outside the method body class Myinner{int Y=4;public String toString () {return "outersize:" +size+ "LocalVar:" + finallocalvar+ "\nthis.y=" +THIS.Y;}} return new Myinner ();} public static void Main (string[] args) {Object obj = new MyOuter3 (). Makeinner (47); System.out.println (Obj.tostring ());}} /* Anonymous Inner class: The ultimate purpose of defining a class is to create an instance of a class, but if an instance of a class is used only once, the definition of the class and the creation of the class can be put together, or in a fixedAt the same time as the semantic class, you create a class that has no name defined in this way and becomes an anonymous inner class. The general format for declaring and constructing anonymous inner classes is as follows: New Classorinterfacename () {class Body} 1. An anonymous inner class can inherit a class or implement an interface, where Classorinterfacename is the class name inherited by an anonymous inner class or an implemented interface Name. But an anonymous inner class cannot implement an interface and inherit a class at the same time.
It is also not possible to implement multiple interfaces. If an interface is implemented, the class is a direct subclass of the object class, the anonymous class inherits a class or implements an interface, and the extends and implements keywords are not required. 2. Because the anonymous inner class does not have a name, the construction method cannot be defined in the class body, and an instance of the class cannot be created because the class name is not known and the keyword cannot be used. In fact, the definition, construction, and first use of anonymous inner classes occur in the same place.
In addition, the upper formula is an expression that returns a reference to an object, so you can use it directly or copy it to an object variable. Example: TypeName obj=new Name () {This is a class body} as well, you can also use the constructed object as a parameter for the call. Example: SomeMethod (new Name () {This is a class body}), */public class MyOuter4 {private int size=5;public Object makeinner (int Localva R) {Final int finallocalvar = Localvar;return new Object () {public String toString () {return "outersize=" +size+ "\ Nfinallocalvar= "+finallocalvar;}};} public static void Main (String args[]) {Object obj=new MyOuter4 (). Makeinner (67); System.out.println (Obj.tostring ());}}

  

Four internal classes of Java

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.