Java Internal classes

Source: Internet
Author: User
Tags static class

Defining a class inside another class is an inner class.

The basic inner class.
public class Innerbasic {

private int id;
protected String name;
static int age;

Abstract class Inner {
public String inner;

void Test () {
This.inner = name;
}
}

Private class PInner {

}

Final class Inners {
String iname = name;
int id = new Innerbasic (). ID;

Innerbasic Getout () {
return innerbasic.this; Returns a reference to an external class object
}
}

Static Class Innert {
int IDT = age;
String s = new Innerbasic (). Name;

Object Test () {
Class Testt {
void xxx () {
SYSTEM.OUT.PRINTLN ("xxx");
}
}
return new Testt ();
}

}

public static void Test1 () {
Innert inner = new Innert (); Static
Inners inners = new Innerbasic (). New Inners ();
}

public void Test () {
Innert Innert = new Innert (); Static
Innerbasic.inners inner = new Innerbasic.inners ();
}
}


Local inner class
Package com.javase.innerclass;

public class Methodinnerclass {
Public IService GetService (final String s) {
Final int count = 100; Can use count cannot be modified, final/here looks like a closure, but completely different. This method ends, and the count scope ends.
Final person person = new person ();
Class Innerservice implements IService {
String name;
Innerservice (String name) {
THIS.name = name;
}

@Override
public void Test () {
THIS.name = s;
System.out.println ("Method inner test" + count + name +person.province);
}
}
return new Innerservice (s);
}
}

Anonymous inner class//For anonymous classes, the actual effect of instance initialization is the constructor. Instance initialization methods cannot be overloaded, so there can be only one constructor.
Anonymous inner classes are somewhat restricted compared to formal inheritance, because anonymous inner classes can extend classes or implement interfaces, but not both.
public class Anonymityclass {

public void print () {
System.out.println ("print");
}

IService Test () {
New Anonymityclass () {

}.print ();

comparator<person> Comparator = new Comparator<person> () {
@Override
public int Compare (person O1, person O2) {
Return (O1.id > O2.id 1:-1);
}
};

list<person> list = new arraylist<person> ();
Collections.sort (list, comparator);

return new IService () {
@Override
public void Test () {

}
};
}
}












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.