anonymous-class

Java anonymous class that implements ActionListener?

旧巷老猫 提交于 2019-11-27 18:55:52
问题 I was recently doing a programming assignment that required us to implement in code a program specified by a UML diagram. At one point, the diagram specified that I had to create an anonymous JButton that displayed a count (starting at one) and decremented each time it was clicked. The JButton and its ActionListener both had to be anonymous. I came up with the following solution: public static void main(String[] args) { JFrame f = new JFrame("frame"); f.setDefaultCloseOperation(JFrame.EXIT_ON

How to start anonymous thread class

折月煮酒 提交于 2019-11-27 17:48:02
I have the following code snippet: public class A { public static void main(String[] arg) { new Thread() { public void run() { System.out.println("blah"); } }; } } Here, how do I call the start() method for the thread without creating an instance of the thread class? You're already creating an instance of the Thread class - you're just not doing anything with it. You could call start() without even using a local variable: new Thread() { public void run() { System.out.println("blah"); } }.start(); ... but personally I'd normally assign it to a local variable, do anything else you want (e.g.

Dynamic construction of anonymous class confusion

穿精又带淫゛_ 提交于 2019-11-27 17:33:03
问题 I'm trying to make instances of anonymous classes using reflection. But ocassionally I've seen strange behaviour during instantination. Please, look at these similar fragments of code public class HideAndSeek { @SuppressWarnings("unchecked") public static void main(String[] args) throws IllegalAccessException, InstantiationException{ final String finalString = "I'm final :)"; Object object2 = new Object(){ { System.out.println("Instance initializing block"); System.out.println(finalString); }

Is this a variation of an anonymous inner class?

自作多情 提交于 2019-11-27 16:16:30
Here is an example JPanel panel = new JPanel(){ @Override protected void paintComponent(Graphics g){ // do stuff } @Override public Dimension getPreferredSize(){ // do stuff } }; Would this just be a variation of an anonymous inner class, or is it something else entirely? Yes that is an anonymous inner class You may be confused about the anonymity of the class because at first blush it looks like you're defining panel to be an instance of JPanel. However, that's not what you are doing. Instead you are defining a sub-class of JPanel, which is a new class and creating panel to be an instance of

“Variable example might not have been initialized” in anonymous class

别说谁变了你拦得住时间么 提交于 2019-11-27 16:11:56
This self-answered question was inspired by Variable 'snackbar' might not have been initialized . I felt that there was more detail which would be better added separate from that specific question. Why can the following code not be compiled? public class Example { public static void main(String[] args) { final Runnable example = new Runnable() { @Override public void run() { System.out.println(example); // Error on this line } }; } } Compilation error: error: variable example might not have been initialized This occurs because of the way that anonymous classes are implemented. You can see this

Anonymous-Inner classes showing unwanted modifier

倾然丶 夕夏残阳落幕 提交于 2019-11-27 14:55:59
To my understanding, the following code should have printed true . However, when I ran this code it is printing false . From Java docs of Anonymous Classes 15.9.5. : An anonymous class is always implicitly final public class Test { public static void main(String args[]) { Object o = new Object() { }; System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers())); } } Can some one please help me understand this behavior. Note that the wording in the JLS of that particular section has changed significantly since then. It now (JLS 11) reads: 15.9.5. Anonymous

Anonymous vs named inner classes? - best practices?

二次信任 提交于 2019-11-27 11:19:16
I have a class, let's call it LineGraph, that renders a line graph. I need to subclass it, but the derived class is only used in one place and is coupled to the class that uses it. So I am using an inner class. I see two ways to do this: Anonymous inner class public class Gui { LineGraph graph = new LineGraph() { // extra functionality here. }; } Named inner class public class Gui { MyLineGraph graph = new MyLineGraph(); private class MyLineGraph extends LineGraph { // extra functionality here. } } I am not a fan of anonymous inner classes, because frankly I just think it looks really ugly.

Java reflection: How can I retrieve anonymous inner classes?

佐手、 提交于 2019-11-27 09:45:39
I have an anonymous inner class inside another class ( SomeClass ). Both SomeClass.class.getClasses() and SomeClass.class.getDeclaredClasses() return empty arrays. I couldn't find some hints on this in Class ' Javadocs. Can anonymous inner classes be retrieved using reflection in some way? What else are notable differences between anonymous inner classes and normal inner classes? If it's using reflection, it's probably a really bad idea. Leaving that aside, I believe you can additional inner classes at runtime, so it doesn't make sense to list classes that may not have been thought of yet.

Local variable access to inner class needs to be declared final

╄→尐↘猪︶ㄣ 提交于 2019-11-27 08:58:13
I got a problem of local variable access to inner class need to be declared final. It is from method createGrids() -> " squares[i][j] = 0; " that i is a local variable that need to be declared final. I don't know why and I have added final in fields but it is not working as well. import java.util.ArrayList; import java.util.Random; //omitted public class Minesweeper{ private JFrame frame; private int cols = 9; private int rows = 9; public static final int GRID_HEIGHT = 9; public static final int GRID_WIDTH = 9; final JButton[][] grids = new JButton[GRID_WIDTH][GRID_HEIGHT]; final int [][]

Java anonymous class efficiency implications

心已入冬 提交于 2019-11-27 08:50:21
Is there any difference in efficiency (e.g. execution time, code size, etc.) between these two ways of doing things? Below are contrived examples that create objects and do nothing, but my actual scenarios may be creating new Threads, Listeners, etc. Assume the following pieces of code happen in a loop so that it might make a difference. Using anonymous objects: void doSomething() { for (/* Assume some loop */) { final Object obj1, obj2; // some free variables IWorker anonymousWorker = new IWorker() { doWork() { // do things that refer to obj1 and obj2 } }; } } Defining a class first: void