Access between static internal classes and non-static inner classes

Source: Internet
Author: User

Package Cn.hncu.day3.inner;

public class Outer {
private int size;

public void aa () {
size++;
SYSTEM.OUT.PRINTLN (size);
}
public void Bb () {
This object is shared, and no additional outer object is required to call AA ()
AA (); Equivalent to THIS.AA ();
}

Non-static inner class 1---demonstrates the basic usage of a non-static inner class
Can be understood as a member of an external class, like a method (including a parameter)---object-level
public class inner{
private int counter=10;
private int size=5;//Inner class can hide member variables of external classes
public void Dosth () {
size++;//can access the member variables of the external class, even if private---is actually the same as the function
SYSTEM.OUT.PRINTLN (size);

The size of the outer class is hidden, and if you must access it, you can use the following method
This.size is an inner class, and Outer.this.size is an external class.
System.out.println (Outer.this.size);
}
}

Non-static inner class 2----I. Demonstrates access between non-static sibling internal classes---object-level
public class inner2{
public void Dosth () {
External class objects are not used because the This object is shared
Inner in = new Inner ();//equivalent to This.new Inner ()
System.out.println (in.counter);//access to private members of the sibling inner class

Iv. accessing a static sibling inner class in a non-static inner class
Inner3 in3 = new Inner3 ();
In3.dosth ();

}

Class a{
private int a=5;
}
}

Static inner class---is very similar to the use of static methods---class-level
Iii. Accessing a non-static inner class in a static inner class
Private
public static Class inner3{
int num=10;
public void Dosth () {
Outer out = new Outer ();
Inner in = Out.new Inner ();
In.dosth ();
}
public static void Fun () {
System.out.println ("aaaaaa");
}
}
Ii. accessing a sibling static inner class in a static inner class
private Static Class inner4{
int num=10;
public void Dosth () {
Inner3 in = new Inner3 ();
In.dosth ();
}
}

public static void Main (string[] args) {
Outer out = new Outer ();
Out.aa ();

Non-static inner class, to construct an inner class object in the same way as an "object of an external class. New"
Inner in = Out.new Inner ();
In.dosth ();

Inner2 in2 = Out.new Inner2 ();
In2.dosth ();
}


}

-----------------------------------------------------------------------

Package Cn.hncu.day3.inner;

public class Outer2 {

For other classes, to access a static or non-static inner class, static and non-static methods, the rules are the same
public void aa () {
2 Accessing a static inner class in another class
Outer.Inner3.fun ();//static method
Outer.inner3 in = new Outer.inner3 ();
In.dosth ();

3 Accessing a non-static inner class in another class
Outer out = new Outer ();
Outer.Inner in2 = Out.new Inner ();
In2.dosth ();
}

public static void Main (string[] args) {
1 Note: Private internal classes cannot be accessed by other external classes
Outer.inner4 in0 = new Outer.inner4 ();//error

2 Accessing a static inner class in another class
Outer.Inner3.fun ();//static method
Outer.inner3 in = new Outer.inner3 ();
In.dosth ();

3 Accessing a non-static inner class in another class
Outer out = new Outer ();
Outer.Inner in2 = Out.new Inner ();
In2.dosth ();

}

}

Access between static internal classes and non-static inner 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.