Java internal class

Source: Internet
Author: User

Java internal class

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

Definition: place a class into the internal definition of another class. The class defined internally is called an internal class (also some are nested classes ), classes that contain internal classes are called external classes (also known as host classes ).

1. Non-static internal class

Internal classes without static modification are non-static internal classes.

Note:. Java does not allow defining static members in non-static internal classes

B. When calling an instance method in a non-static internal class, there must be a non-static member internal class instance, and the non-static internal class instance must be parasitic inside the external instance.

C. If the external class member variables have the same name as the local variables of the internal class member variables and the methods in the internal class, you can use this, the external class name. this as a limitation to distinguish.

Public class Test {private String prop = "instance variable of the external class"; public class InClass {private String prop = "instance variable in the internal class"; public void info () {String prop = "local variable"; System. out. println ("instance variable value of the external class:" + Test. this. prop); System. out. println ("instance variable value of the internal class:" + this. prop); System. out. println ("local variable:" + prop) ;}} public void test () {InClass ic = new InClass (); ic.info ();} public static void main (String [] args) {/** Test ts = new Test (); * ts. test (); * The upper and lower code functions the same */new Test (). test ();}}

Use non-static internal classes:

Syntax format of the internal class defined outside the external class: OuterClass. InnerClass VarName

Syntax for creating non-static internal class instances outside the external class: OuterInstace. new InnerConstructor ()

Class Out {class In {public In (String msg) {System. out. println (msg) ;}} public class CreateInnerInstance {public static void main (String [] args) {// OuterClass. innerClass varName = new OutInstance. new InnearConstructor (); Out. in in = new Out (). new In ("test information");/* Out. in in; * Out out = new Out (); * in2 = out. new In ("test information ");

*/
}}

 

2. static internal class

Static internal classes are internal classes modified with static, also known as internal classes of the class.

A. Static internal classes cannot access instance members of external classes. They can only access class members of external classes (both static and modified ).

C. The external class still cannot access members in the static internal class, but the Class Name of the static internal class can be used as the caller to access class members in the static internal class, you can also use static internal class objects as callers to access instance variables in static internal classes.

Public class StaticInnerClassTest {private int prop = 5; private static int prop1 = 6; static class InClass {public void accessOuterProp (){

Private static int prop = 5;
Private int prop1 = 6;
// System. out. println (prop); // The Code contains errors. The static internal class cannot access non-static members of the external class.

System. out. println (prop1 );}}
Public void accessInnerPro (){
// Use the Class Name of the static internal class to access class members in the static internal class


System. out. println (InClass. prop );
// Use static internal class objects as callers to access instance variables in static internal classes
SYstem. out. println (new InClass (). prop1 );
}
}

Use static internal classes other than external classes: (similar to non-static comparisons)
Syntax: new OuterClass. InnerConstructor ()

Class Out {static class In {public In () {System. out. println ("static internal class constructor") ;}} public class CreateStaticInnerInstance {public static void main (String [] args) {Out. in in = new Out. in ();}}

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

Defines the format of anonymous internal classes:

New Implementation interface | parent class Constructor (real parameter list) {class body part}

It can be seen that an anonymous internal class must inherit a parent class or implement an interface.

Interface Product {public Double getPrice (); public String getName ();} public class AnonymTest {public void ce (Product p) {System. out. println ("purchased one" + p. getName () + ", spent" + p. getPrice ();} public static void main (String [] args) {AnonymTest at = new AnonymTest ();. ce (new Product () {@ Override public Double getPrice () {return 0.5 ;}@ Override public String getName () {return "Spicy ";}});}}

 

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.