Java Internal Classes

Source: Internet
Author: User

One: The inner class is defined within the class body, the method body part, or even a class within a code block that is smaller than the method body (ifstatement inside, etc.)1. static inner class (the simplest form in the inner class)1declared in the body of the class, outside the method body, and using the static decorated inner class2access characteristics can be analogous to static variables and static methods3. An instance of an external class is created independently of an instance of an external class to build an inner classNewOuter.Inner (); Building an instance of an inner class inside an external classNewInner (); 4the static inner class body can access all static members of the outer class directly, including the private/** * @authorGress * Static inner class*/ Public classStaticinnertest { Public Static voidMain (string[] args) { staticouter.staticinner si = new Staticouter.staticinner ();        Si.test2 (); //StaticOuter.StaticInner.test ();System.out.println ("SI.B =" +si.b); System.out.println ("SI.A =" +SI.A); //System.out.println ("staticouter.b =" +staticouter.b); Error here    } } classStaticouter {Private intA = 100;Private Static intb = 150; Public Static voidTest () {System.out.println ("Outer Static Test ...");} Public  voidtest2 () {System.out.println ("Outer INSTABCE test ...");} Static classStaticinner { Public  intA = 200; Static intb =300;  Public Static voidTest () {System.out.println ("Inner Static Test ..."); }         Public  voidtest2 () {System.out.println ("Inner instance test ...");            staticouter.test (); New Staticouter (). Test2 (); System.out.println ("Staticouter.b =" +staticouter.b); }    }}    2. member Inner Class (Instance inner Class)1. There are no internal classes that use the static modifier. 2. declarations of static variables and static methods are not allowed in members ' inner classes.        static can only be used on declarations of statically constant constants. 3members inside a class can access all members of the external class (variables, methods), including private members, if there is an instance variable with the same name as the outer class defined in the inner class, Access: Outerclass. This.outermember; 4building an instance of an inner class requires that an instance of the outer class must first exist outside the outer class /External class static method: New Outer (). New Inner (); Instance methods for external classes:NewInner ();  This.NewInner ();** *@author gress*instance Inner class* */classmemberouter{PrivateString S1 = "Outer Instancemar"; PrivateString s2 = "Outerstatic s2";  Public voidsetS1 (String s1) { This. S1 =S1; new Memberouter (). New Memberinner (); This.new Memberinner ();  //The Memberouter is already instantiated, so you can use this        NewMemberinner (). Test2 (); }     Public Static voidtest2 () {NewMemberouter ().NewMemberinner (); /*this.new Memberinner (); * The Memberouter is not instantiated at this time, so it is not possible to use this * static before the Memberouter constructor, so this cannot be used at this time **/            }      classmemberinner{String S1= "Inner Instancemae"; Static FinalString S4 = "Static Final Memberinner"; voidtest2 () {System.out.println ("S1 =" +S1); System.out.println ("Outter MemberOuter.this.s1 =" + Memberouter. This. S1); System.out.println ("S2 =" +S2); }    }    }  Public classMemberinnertest { Public Static voidMain (String args []) {/*memberouter.memberinner mm = new Memberouter (). New Memberinner (); Mm.test2 ();*/memberouter Mo=NewMemberouter (); Mo.sets1 (""); }}

It is important to note, however, that when a member inner class has a member variable or method with the same name as an external class, there is a hidden phenomenon, 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 to access it in the following form:

外部类. this .成员变量 外部类. this .成员方法 Although members of the inner class can access the members of an external class unconditionally, the external class would like to access members of the member's inner class but not so much. In an external class, if you want to access members of a member's inner class, you must first create an object of the member's inner class, and then access it by referring to the object
3. Local inner class:1. defined in the method body, even in a code block smaller than the method body 2. Analogy local variables. 3The local inner class is one of the least used forms in all inner classes. 4The members of an external class that a local inner class can access are different depending on the method body in which they are located. If you are in a static method: You can access all static members of an external class, including private if in an instance method: You can access all members of the external class, including private.          local Inner classes can access local variables defined in the method, but require that the local variable must use the final decoration.           ** *@author gress local inner class* */ Public classLocalinnertest {Private intA = 1; Private Static intb = 2;  Public voidTest () {Final intc = 3; classLocalinner { Public voidadd1 () {System.out.println ("A=" +a); System.out.println ("B=" +b); System.out.println ("C=" +c); }        }        NewLocalinner (). ADD1 (); }     Static  Public voidtest2 () {Final int d = 5; classLocalInner2 { Public voidadd1 () {//System.out.println ("a=" + a);System.out.println ("b=" +b); System.out.println ("C=" +d); }        }        NewLocalInner2 (). ADD1 (); }      Public Static voidMain (String args[]) {//localinnertest () LC = new Localinnertest ();        Newlocalinnertest (). Test2 (); Newlocalinnertest (). Test (); }}    4. Anonymous inner class1. A local inner class without a name. 2. No class,Interface,Implements, extends keyword3. No constructors. 4. Generally implicitly inherits a parent class or implements an interface5. A very vivid example of what the foodie teacher said/**         * @authorGress Anonymous inner class, I will only use the class once * if I want to eat a bubble noodles, but I can not build a factory, manufacturing a pipeline, the production of a bag of noodles after the not to use this bubble noodle factory * So here is the anonymous inner class, and we built the bubble noodle factory like One of the classes built here is the same pencil pencil class*/** *@author gress Anonymous inner class, I will only use the class once*  *if I want to eat a bubble noodles, but I can not build a factory, manufacturing an assembly line, the production of a bag of noodles after the use of this bubble noodle factory*So here's the anonymous inner class, and the Bubble noodle factory we built is like a class pencil pencil class built here.*/InterfacePen { Public voidwrite ();} classPencilImplementsPen {@Override Public voidwrite () {//the factory of the pencil    }} class Person {public void User (pen pen) {pen.write (); }}  Public classAnyinnertest { Public Static voidMain (String args[]) {person Guo=NewPerson (); Guo.user ( New Pen () {@Override public void write () {System.out.println ("Write Child" ));    }        }); }} II:Abstractonly single inheritance1. Methods: Abstract methods cannot have method body abstract methods where the class must be abstract class abstract method exists is overwritten, if the subclass inherits abstract classes with abstract methods, so the abstract method must be overwritten2. class: Abstract class abstract class can  not be instantiated abstract class is an abstraction of the class, abstract the common characteristics and behavior of the abstract class exists to be inherited, to implement code reuse abstract class can have abstract methods can also have no abstract method, can Like a normal class with member variables and methods if a class inherits an abstract class, you must implement an abstract method that abstracts the parent class, or the subclass is also an abstract method three:Interfacecan be implemented more (inherited)1An interface is an abstraction from an abstract class , and an interface is the ultimate abstraction of an abstract class. 2.all the methods in the interface are public Abstracht, all the variables in the interface are publicStatic Final3The interface is primarily used to define standards. 4. The interface can inherit multiple.    A class can implement multiple interfaces. 5the existence of the interface itself can circumvent the operational characteristics that Java cannot inherit more. Interfaceusbdevice{String Type= "USB"; voiddriver (); }        classmouse{Select (); }    classUsbmouseextendsMouseImplementsusbdevice{}classcomputer{voidUse (Usbdevice USB) {usb.driver (); }    }

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.