Java Internal classes

Source: Internet
Author: User

member Inner class

public class User {
private int age=12;
member Inner class
Class in {
public void print () {
System.out.println (age);
}
}
}

public class Test {

public static void Main (string[] args) {
User U=new user ();

User.in i=u.new in ();
I.print ();
}

}

Variable access

public class User {
private int age=12;
member Inner class
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:" + User.this.age);
}
}
}

Static Inner class

classOut {

    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();    }}Method Inner Class

classOut {

    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 out = new Out();        out.Print(3);    }}

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.

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.