Java Internal class Learning

Source: Internet
Author: User

Inner class: A class defined in another class

Java Internal class classification:
Static internal classes (static inner class)
Member Inner classes (member inner Class)
Local inner classes (local inner class)
Anonymous internal classes (anonymous inner class)

Properties for each class:

Static internal classes (accessed in the same way as static methods):
Only static member variables and static methods of external classes can be accessed

Create:
(method i) Staticinner.inner Inner = new Staticinner.inner ();

(Method II) Import note. Staticinner.inner;
Inner Inner = new Inner ()

member Inner Class (Access method equals member method)

Access to all member variables and methods in the external class

Create: To create a member inner class you first create an external class (for example, to invoke a member method, you must first initialize an object)
Memberinner.inner Memberinner = new Memberinner (). New Inner (); That's right
Memberinner.inner Memberinner = new Memberinner.inner (); Error: Create member inner class to create an external class first


Local inner class
Local inner classes are defined in a method and are smaller than the range of methods. Is the least used type in an inner class.

Like local variables, it cannot be modified by public, protected, private, and static.

Only local variables of the final type defined in the method can be accessed, as well as member variables and member methods in external classes

Local inner classes are defined in methods, so they can only be used in methods, that is, you can only generate instances of local inner classes within a method and invoke their methods.


Anonymous inner class
The anonymous inner class is a local inner class without a name, not using the keyword class, extends, implements, and no constructor method.

An anonymous inner class implicitly inherits a parent class or implements an interface.

Anonymous inner classes are used more often as a method parameter.

Code:

Package note;

Import note. Staticinner.inner;

/**
* Static inner class {@docRoot static inner class can only access static member variables and static methods of external classes (regardless of private/public)}
* @author zhaobing
*
*/
Class staticinner{

/**
* Define a static variable to allow static internal classes to access
*/
private static String name= "Staticinner";

public static void Printfexternal () {
SYSTEM.OUT.PRINTLN ("External class static Method");
}

Static Class inner{
void printf () {
System.out.println ("This is external class static member (name):" + name);
Printfexternal ();
}
}
}

/**
* Member inner class {@docRoot static inner class can access only member variables and methods of external classes (regardless of private/public)}
* @author zhaobing
*
*/
Class memberinner{

Private String name = "Memberinner";

public void Printfexternal () {
System.out.println ("External class Method");
}

Class inner{

public void printf () {
System.out.println ("This is external class member (name):" + name);
Printfexternal ();
}
}
}

/**
* Defines a class in a method of an outer class
* Local inner class {@docRoot member variables and methods of the static outer class (regardless of private/public) but accesses non-final variables in the method of the outer class}
* @author Zhaobing
*
*/
Class localinner{

Private String name = "Localinner";
private int age = 10;
private void P () {
System.out.println ("test");
}

public void printfexternal () {
int b=3;
final int c=1;

Class inner{
public void printf (String name) {
System.out.println (name+c+age);
System.out.println (Name+a+age); Error cannot access method non-final variable
P ();
}
}
Inner Inner = new Inner ();
inner.printf (name);
System.out.println ("External class method");

}
}

public class Innerclassdemo {

public static void Main (String args[]) {

/**
* Static internal class test
*/
Inner staticinner=new Inner ();
Staticinner.printf ();
Staticinner.inner Inner = new Staticinner.inner (); It can also be created in this way, which is created without import imports

/**
* Member internal class test
*/
Memberinner.inner Memberinner = new Memberinner.inner (); Error: Create member inner class to create an external class first
Memberinner.inner Memberinner = new Memberinner (). New Inner ();
Memberinner.printf ();

/**
* Member internal class test
*/
Localinner Localinner = new Localinner ();
Localinner.printfexternal ();

/**
* Anonymous Inner class
*/
Anonymousinner Anonymousinner = new Anonymousinner ();
anonymousinner.printf (New Integer (3));
}

}

Class anonymousinner{

Private String name = "Anonymousinner";

public void printf (Integer i) {
System.out.println ("I:" +i);
}

}

Java Internal class Learning

Related Article

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.