Summary of internal classes in Java

Source: Internet
Author: User

The inner class is not very well understood, but it is actually a class that contains another class.


As a person is composed of physical results, such as the brain, limbs, organs, or an internal class equivalent to one of the organs, such as the heart: it also has its own properties and behavior (blood, beating)


Obviously, it is not possible to use attributes or methods to represent a heart unilaterally, but a class


And the heart is in the body, just as the same inner class is inside the outside.



Example 1: The basic structure of an inner class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

External class

Class Out {

private int age = 12;

Inner class

Class in {

public void print () {

System.out.println (age);

}

}

}

public class Demo {

public static void Main (string[] args) {

Out.in in = new Out (). New in ();

In.print ();

or access by sowing

/*

out = new Out ();

Out.in in = Out.new in ();

In.print ();

*/

}

}

Run Result: 12


From the above example, it is not difficult to see that the inner class actually seriously destroys the good code structure, but why use the inner class?


Because inner classes are free to use member variables of external classes (including private ones) without generating objects of external classes, this is the only advantage of inner classes


Like the heart can directly access the body's blood, rather than through the doctor to draw the blood



After compiling the program, two. class files are generated, respectively Out.class and Out$in.class


Where $ represents the out.in in the above program.


Out.in in = new Out (). The new in () can be used to generate an object of the inner class, which has two small knowledge points to note


1. The first out is to indicate which outer class the inner class object needs to be generated


2. An object of an outer class must be preceded to generate an object of the inner class, because the inner class is intended to access member variables in the outer class



Example 2: Variable Access form in an inner class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Class Out {

private int age = 12;

Class in {

private int age = 13;

public void print () {

int age = 14;

SYSTEM.OUT.PRINTLN ("local variable:" + age);

System.out.println ("Inner class variable:" + this.age);

SYSTEM.OUT.PRINTLN ("External class variable:" + Out.this.age);

}

}

}

public class Demo {

public static void Main (string[] args) {

Out.in in = new Out (). New in ();

In.print ();

}

}

Operation Result:


Local variables: 14

Internal class variable: 13

External class variables: 12


As you can see from Example 1, an inner class that has no member variables and local variables of the same name, the inner class accesses the member variables of the outer class directly without specifying Out.this. Property name


Otherwise, local variables in the inner class will overwrite the member variables of the outer class


The member variable that accesses the inner class itself is available with the this. property name, and accessing the member variables of the external class requires the use of Out.this. Property name



Example 3: Static inner class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Class Out {

private static int age = 12;

Static class in {

public void print () {

System.out.println (age);

}

}

}

public class Demo {

public static void Main (string[] args) {

Out.in in = new out.in ();

In.print ();

}

}

Run Result: 12


As you can see, if you statically internal static, the inner class can only access static member variables of the outer class, with limitations


Second, because the inner class is statically, the out.in can be viewed as a whole, and can be directly new to the object of the inner class (access to static through the class name, it doesn't matter if the external class object is generated)



Example 4: Private Inner class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

Class Out {

private int age = 12;

Private class in {

public void print () {

System.out.println (age);

}

}

public void Outprint () {

New in (). print ();

}

}

public class Demo {

public static void Main (string[] args) {

This method is not valid

/*

Out.in in = new Out (). New in ();

In.print ();

*/

out = new Out ();

Out.outprint ();

}

}

Run Result: 12


If an inner class only wants to be manipulated by a method in an external class, you can use private to declare the inner class


In the above code, we have to create an in class object inside the out class, and we can no longer use out.in in = new Out (). New in () creates an object of the inner class


That is, the inner class at this point is controlled only by the outer class


As is, my heart can only be controlled by my body, others cannot access it directly



Example 5: Method Inner class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Class Out {

private int age = 12;

public void Print (final int x) {

Class in {

public void Inprint () {

SYSTEM.OUT.PRINTLN (x);

System.out.println (age);

}

}

New in (). Inprint ();

}

}

public class Demo {

public static void Main (string[] args) {

out = new Out ();

Out. Print (3);

}

}

Operation Result:


3

12


In the above code, we move the inner class into the method of the outer class, and then regenerate it into an inner class object in the method of the outer class to invoke the inner class method


If we need to pass in a parameter to a method in the outer class at this point, then the method parameter of the outer classes must use the final definition


As for final, there's no special meaning here, it's just a representation.



Summary of internal classes in 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.