anonymous-class

Stop a periodic task from within the task itself running in a ScheduledExecutorService

时光怂恿深爱的人放手 提交于 2019-11-26 20:35:43
问题 Is there a nice way to stop the repetition of task from within the task itself when running in a ScheduledExecutorService? Lets say, I have the following task: Future<?> f = scheduledExecutor.scheduleAtFixedRate(new Runnable() { int count = 0; public void run() { System.out.println(count++); if (count == 10) { // ??? cancel self } } }, 1, 1, TimeUnit.SECONDS); From outside, it is easy to cancel via f.cancel(), but how can I stop the repetition at the specified place? (Passing the Future

Does Python have something like anonymous inner classes of Java?

你说的曾经没有我的故事 提交于 2019-11-26 19:56:50
问题 In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class. Suppose that you want create a subclass of OptionParser that overrides only a single method (for example exit() ). In Java you can write something like this: new OptionParser () { public void exit() { // body of the method } }; This piece of code creates a anonymous class that extends OptionParser and override only the exit() method. There is a

Java lambdas 20 times slower than anonymous classes

谁说我不能喝 提交于 2019-11-26 18:45:53
I've seen a lot of questions here about Java lambdas performance, but most of them go like "Lambdas are slightly faster, but become slower when using closures" or "Warm-up vs execution times are different" or other such things. However, I hit a rather strange thing here. Consider this LeetCode problem : Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. The problem was tagged hard, so I assumed that a linear approach is not what they want there. So I

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

点点圈 提交于 2019-11-26 18:36:52
问题 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

Is this a variation of an anonymous inner class?

大城市里の小女人 提交于 2019-11-26 18:35:41
问题 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? 回答1: Yes that is an anonymous inner class 回答2: 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

Access “this” from Java anonymous class

﹥>﹥吖頭↗ 提交于 2019-11-26 17:18:49
Given the following code: public interface Selectable { public void select(); } public class Container implements Selectable { public void select() { ... } public void createAnonymousClass() { Selectable s = new Selectable() { public void select() { //see comment below. } }; } } I want to access Container.select() from within my anonymous class' select() method. However, this.select() would again call the anonymous class' select() method. My suggestion would be: Introduce a field into Container, e.g. private Container self = this; Now I can access Container.select() by calling self.select()

Anonymous-Inner classes showing unwanted modifier

别等时光非礼了梦想. 提交于 2019-11-26 16:57: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. 回答1: Note that the wording in the JLS

Why is an anonymous inner class containing nothing generated from this code?

二次信任 提交于 2019-11-26 15:24:45
package com.test; public class OuterClass { public class InnerClass { public class InnerInnerClass { } } public class InnerClass2 { } //this class should not exist in OuterClass after dummifying private class PrivateInnerClass { private String getString() { return "hello PrivateInnerClass"; } } public String getStringFromPrivateInner() { return new PrivateInnerClass().getString(); } } When run through javac on the command line with Sun JVM 1.6.0_20 , this code produces 6 .class files: OuterClass.class OuterClass$1.class OuterClass$InnerClass.class OuterClass$InnerClass2.class OuterClass

Can we create an instance of an interface in Java? [duplicate]

百般思念 提交于 2019-11-26 14:56:51
This question already has an answer here: Can we create an object of an interface? 5 answers Is it possible to create an instance of an interface in Java? Somewhere I have read that using inner anonymous class we can do it as shown below: interface Test { public void wish(); } class Main { public static void main(String[] args) { Test t=new Test() { public void wish() { System.out.println("output: hello how r u"); } }; t.wish(); } } cmd> javac Main.java cmd> java Main output: hello how r u Is it correct here? Magnus Yes, your example is correct. Anonymous classes can implement interfaces, and

How to pass parameters to anonymous class?

倖福魔咒の 提交于 2019-11-26 14:53:28
Is it possible to pass parameters, or access external parameters to an anonymous class? For example: int myVariable = 1; myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // How would one access myVariable here? } }); Is there any way for the listener to access myVariable or be passed myVariable without creating the listener as an actual named class? Technically, no, because anonymous classes can't have constructors. However, classes can reference variables from containing scopes. For an anonymous class these can be instance variables from the