Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.

Similar presentations


Presentation on theme: "Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP."— Presentation transcript:

1 www.javacup.ir Sadegh Aliakbary

2 Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to info@javacup.irinfo@javacup.ir 2

3 Agenda Inner classes Anonymous inner class 3

4 Class Types class FriendlyClass{ } public class OuterClass { private int value; public class Inner{ public void f(){ … } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source4 Friendly Class Public class Inner Class

5 Inner Classes Declared in another class Instantiated using a reference object of outer class Has access to this object The inner class can be static No reference object of outer class is needed No access to outer class is provided JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source5

6 public class OuterClass { private int value = 2; class Inner{ public void innerMethod(){ OuterClass.this.value = 5; } public void outerMethod(){ Inner inner = new Inner(); inner.innerMethod(); } public static void main(String[] args) { OuterClass outer = new OuterClass(); System.out.println(outer.value); outer.outerMethod(); System.out.println(outer.value); } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source6 OuterClass.this is implicitly saved in inner object

7 public class OuterClass { private int value = 2; class Inner{ public void f(){ OuterClass.this.value = 5; } public static void main(String[] args) { OuterClass outer = new OuterClass(); OuterClass.Inner inner = outer.new Inner(); System.out.println(outer.value); inner.f(); System.out.println(outer.value); } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source7 Why we need outer reference?

8 class OuterClass { static class Inner{ public void f(){ System.out.println("f() invoked"); } public class MainClass { public static void main(String[] args) { OuterClass.Inner in = new OuterClass.Inner() ; in.f(); } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source8

9 Inner Class Specifiers static final Access Specifiers public private Friendly (no specifier) protected JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source9

10 Anonymous Inner Class An inner class With no name Created once Used once No access to this class from any other place Once created and used JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source10

11 interface Inner{ void innerMethod();} public class OuterClass { private int value = 2; public void outerMethod(){ Inner inner = new Inner() { public void innerMethod() { OuterClass.this.value = 5; } }; inner.innerMethod(); } public static void main(String[] args) { OuterClass outer = new OuterClass(); System.out.println(outer.value); outer.outerMethod(); System.out.println(outer.value); } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source11

12 Anonymous Inner Class Usually implements an interface Or extends another class And Overrides some special method Main Application : Event Handlers JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source12

13 public class MyFrame extends JFrame{ JButton button; JTextField textbox; public MyFrame(){ button = new JButton("Click!"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, textbox.getText()); } }); add(button); textbox = new JTextField(10); add(textbox); } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source13

14 JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source14


Download ppt "Www.javacup.ir Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP."

Similar presentations


Ads by Google