anonymous-inner-class

The Anonymous Class Conundrum

冷暖自知 提交于 2019-11-29 17:51:59
I think I understand the basics of Anonymous classes but I'd like to clarify something. when I have a syntax such as this class A { class AnonymousClass1 Implements ActionListener{} } class A { public A() { JButton btn = new JButton(); btn.addActionListener( new ActionListener(){} ); } } If the anonymous class is actually an inner class of class A, as in the first example: in theory, is the semantics right? What happens exactly? I think when the java file is compiled, a .class file is created for the anonymous class so it can be referenced (but I couldn't find it). When an object of A is

Declaring a method when creating an object

∥☆過路亽.° 提交于 2019-11-29 16:01:12
问题 Why first way is correct, but second isn't? First way: new Object() { public void a() { /*code*/ } }.a(); Second way: Object object = new Object() { public void a() { /*code*/ } }; object.a(); And where can I find more information about it? 回答1: java.lang.Object has no a methods declared (2), while the anonymous class returned by the class instance creation expression new Object() { public void a() {} } does (1). Use Java 10's local variable type inference ( var ) to make the second option as

access variables of outer class in Java

ⅰ亾dé卋堺 提交于 2019-11-29 03:27:27
in Java android application how can i access variables of outer class from the inner anonymous class ? Example: ProgressDialog dialog = new ProgressDialog(this); ..... send.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //here i'd like to do something with **dialog** variable ....... } }); If the dialog variable is a field of the outer class, you can use this prefixed with the outer class name ( a qualified this ): send.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ProgressDialog dlg = OuterClass.this.dialog; ....... } });

Difference between new Test() and new Test() { }

谁说胖子不能爱 提交于 2019-11-28 18:13:06
What is the difference between these two ways of instantiating new objects of a class as follows: Test t1=new Test(); Test t2=new Test(){ }; When I tried the following code, I could see that both objects could access the method foo() , but t2 cannot access the variable x ( variable x cannot be resolved): public class Test { int x=0; public void foo(){ } public static void main (String args[]) { Test t1=new Test(); Test t2=new Test(){ }; t1.x=10; t2.x=20; t1.foo(); t2.foo(); System.out.println(t1.x+" "t2.x); } } Test t2=new Test(); will create the object of Test class. But Test t2=new Test(){ }

Anonymous inner class using an interface in Java

旧巷老猫 提交于 2019-11-28 12:04:14
问题 So, when looking into lambda expressions and using them to substitute anonymous inner classes for EventHandlers in Java, I came across a few anonymous inner classes that made me stop and think. For instance, when writing an anonymous inner class for something that normally implements ActionListener we write myJButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e){ //DO SOMETHING } }); My confusion with this is, ActionListener is an interface so I

Anonymous inner classes in C#

半城伤御伤魂 提交于 2019-11-28 10:45:05
I'm in the process of writing a C# Wicket implementation in order to deepen my understanding of C# and Wicket. One of the issues we're running into is that Wicket makes heavy use of anonymous inner classes, and C# has no anonymous inner classes. So, for example, in Wicket, you define a Link like this: Link link = new Link("id") { @Override void onClick() { setResponsePage(...); } }; Since Link is an abstract class, it forces the implementor to implement an onClick method. However, in C#, since there are no anonymous inner classes, there is no way to do this. As an alternative, you could use

access variables of outer class in Java

女生的网名这么多〃 提交于 2019-11-27 22:08:07
问题 in Java android application how can i access variables of outer class from the inner anonymous class ? Example: ProgressDialog dialog = new ProgressDialog(this); ..... send.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //here i'd like to do something with **dialog** variable ....... } }); 回答1: If the dialog variable is a field of the outer class, you can use this prefixed with the outer class name (a qualified this): send.setOnClickListener(new View

Is an enum constant-specific class body static or non-static?

你说的曾经没有我的故事 提交于 2019-11-27 11:55:07
I have a enum type class: public enum Operation { PLUS() { @Override double apply(double x, double y) { // ERROR: Cannot make a static reference // to the non-static method printMe()... printMe(x); return x + y; } }; private void printMe(double val) { System.out.println("val = " + val); } abstract double apply(double x, double y); } As you see above, I have defined one enum type which has value PLUS . It contains a constant-specific body. In its body, I tried to call printMe(val); , but I got the compilation error: Cannot make a static reference to the non-static method printMe(). Why do I get

Difference between new Test() and new Test() { }

淺唱寂寞╮ 提交于 2019-11-27 11:08:11
问题 What is the difference between these two ways of instantiating new objects of a class as follows: Test t1=new Test(); Test t2=new Test(){ }; When I tried the following code, I could see that both objects could access the method foo() , but t2 cannot access the variable x ( variable x cannot be resolved): public class Test { int x=0; public void foo(){ } public static void main (String args[]) { Test t1=new Test(); Test t2=new Test(){ }; t1.x=10; t2.x=20; t1.foo(); t2.foo(); System.out.println

Java - Interface, instantiating an interface?

夙愿已清 提交于 2019-11-27 06:58:08
问题 So I just found this code example online a while ago and I'm going over it again but quite confused. From looking at it, what I gather (and it might be wrong) is that it passes to the print method in the NumberPrinter class a Printer object. However, the interface is also called Printer, so aren't we instantiating an anonymous class of the Printer interface, defining the methods and then passing it? My basic question is, is my initial assumption correct? And if so I thought you could not