Java Internal classes

Source: Internet
Author: User

1, the inner class can be regarded as the field of the outer class, the modifiers and meanings used are the same as field (field uses the same modifier and method), you can also use Final,static,abstract
2. Generate inner classes in the outer class
Class outer{
Class inner{
}
}

Class test{
public void Main (string[] args) {
Outer o=new Outer ();
Outer.Inner i=o.new Inner ();
}
}
3. Method of accessing the field of an external class in an inner class: OuterClassName.this.FieldName
4. Internal classes defined in the method to access local variables in the method, these variables must be declared as final
5. Non-static inner class cannot declare static method
6, the inheritance of the inner class:
Class car{
Class wheel{
}
}
Class Planewheel extends car.wheel{
This constructor is required to establish a reference relationship between the inner class and the outer class
Planewheel (car car) {
Car.super ();
}
public static void Main (string[] args) {
Car car=new car ();
Planewheel pw=new Planewheel (CAR);
}
}
7, using anonymous internal classes to implement the interface
Interface animal{
void Eat ();
void run ();
}
public class zoo{

Public Animal Getanimal () {
return new Animal () {

public void Eat () {
}
public void Run () {
}
};// “;” Not less
}
}

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.