Java Internal classes

Source: Internet
Author: User

The basic body of the inner class

public class Out {private INT-age = 12;class in {public void print () {System.out.println (age);}}
public static void Main (string[] args) {out.in-in = new-out (). New in (), In.print ();/************************/out out = new Out (); In in2 = Out.new in (); In2.print ();}

Internal classes actually seriously undermine the good code structure, but why use internal classes?

This is the only advantage of an inner class because the inner class is free to use the member variables of the outer class, including the private ones, without generating an object of the outer class. 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

variable access forms in internal classes
public class Out {private INT-age = 12;class in{private int-age = 13;public void print () {int-age = 14; SYSTEM.OUT.PRINTLN ("local variable:" +age);//14system.out.println ("Inner class variable:" +this.age);//13system.out.println ("Outer class variable:" + Out.this.age);//12}}public static void Main (string[] args) {out.in in = new Out (). New in (); In.print ();}}

static inner class
public class out {private static int = 12;static class in{public void print () {System.out.println (age);}} public void print () {New in (). print ();} public static void Main (string[] args) {out.in in = new Out.in (), In.print ();//12out out = new Out (); Out.print ();//12}}

If static inside is statically, 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)

private Inner class
public class Out {private INT-age = 12;private class in{public void print () {System.out.println (age);}} public void Outprint () {New in (). print ();} public static void Main (string[] args) {out.in-in = new-out (). New in (); In.print (),//12out out = new Out (); Out.outprint (); /12}}

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

In the above code, we have to create an in class object inside the Out class, and no longer use out.in in = new Out (). New in () generates 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 and others cannot access it directly.

method Inner Class
public 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 static void Main (string[] args) {out-out = new Out (); Out.print (3);}}

Output 3 12

In the above code, we move the inner class into the method of the outer class and then regenerate the 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 here there is no special meaning, just a form of representation.

Anonymous inner class is also an inner class without a name

Because there is no name, the anonymous inner class can only be used once, and it is often used to simplify code writing

But there is also a precondition for using anonymous inner classes: You must inherit from a parent class or implement an interface

Implementation of anonymous inner class

Public abstract class The person {//abstract type public eat ();}
public class Demo {public static void main (string[] args) {Person person = new person () {@Overridepublic void Eat () {Syste M.out.println ("Eat ~");}; Person.eat ();}}

Using anonymous inner classes on an interface

Public interface Person {//Interface public void Eat ();}
public class Demo {public static void main (string[] args) {//the method in the abstract class person is implemented directly in curly braces so that you can omit a class from the writing person person = new Pers On () {@Overridepublic void Eat () {System.out.println ("Eat ~ ~");}; Person.eat ();}}
Anonymous inner class implementation of the thread class
Thread t = new Thread () {public void run () {for (int i = 1; I <= 5; i++) {System.out.print (i + "")}}}; T.start ();
Anonymous inner class implementation of the Runnable interface
Runnable Runnable = new Runnable () {@Overridepublic void Run () {for (int i = 1; I <= 5; i++) {System.             Out.print (i + ""); }}}; Thread thread = new Thread (runnable); Thread.Start ();


This article from "Avatar" blog, reproduced please contact the author!

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.