4 types of Java inner classes
There are 4 different types of inner classes in Java. This post illustrates them by using 4 simple examples.
1. Static Nested Classes
class Outer { static class Inner { void go() { System.out.println("Inner class reference is: " + this); } } } public class Test { public static void main(String[] args) { Outer.Inner n = new Outer.Inner(); n.go(); } } |
Inner class reference is: [email protected]
2. Member Inner Class
Member class is instance-specific. It has access to all methods, fields, and the Outer's this reference.
public class Outer { private int x = 100; public void makeInner(){ Inner in = new Inner(); in.seeOuter(); } class Inner{ public void seeOuter(){ System.out.println("Outer x is " + x); System.out.println("Inner class reference is " + this); System.out.println("Outer class reference is " + Outer.this); } } public static void main(String [] args){ Outer o = new Outer(); Inner i = o.new Inner(); i.seeOuter(); } } |
Outer x is 100 Inner class reference is [email protected] Outer class reference is [email protected]
3. Method-Local Inner Classes
public class Outer { private String x = "outer"; public void doStuff() { class MyInner { public void seeOuter() { System.out.println("x is " + x); } } MyInner i = new MyInner(); i.seeOuter(); } public static void main(String[] args) { Outer o = new Outer(); o.doStuff(); } } |
x is outer
public class Outer { private static String x = "static outer"; public static void doStuff() { class MyInner { public void seeOuter() { System.out.println("x is " + x); } } MyInner i = new MyInner(); i.seeOuter(); } public static void main(String[] args) { Outer.doStuff(); } } |
x is static outer
4. Anonymous Inner Classes
This is frequently used when you add an action listener to a widget in a GUI application.
button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ comp.setText("Button has been clicked"); } }); |
<pre><code> String foo = "bar"; </code></pre>
Pingback: 免费Simple Java (非常简单的英文) - IT新闻()
Pingback: 免费Simple Java (非常简单的英文) | | Evolution Unit 进化Evolution Unit 进化()
Pingback: 免费Simple Java (非常简单的英文) | 我爱互联网()
Pingback: Java中4种类型的内部类 - 编程 - 开发者()