anonymous-inner-class

Is there a syntax to get the reference to an anonymous inner class from a further anonymous inner class?

血红的双手。 提交于 2019-12-04 07:54:51
Consider this case: public class SomeClass { public void someMethod() { new SomeInterface() { public void someOtherMethod() { new SomeOtherInterface() { new someThirdMethod() { //My question is about code located here. } }; } }; } } Is there a syntax to reference the instance of the anonymous inner class represented by SomeInterface at the commented code? For SomeClass you can do SomeClass.this Is there an equivalent to get the implementation of SomeInterface? If not, of course you can just define a final local variable in the SomeInterface implementation and reference it, but I was just

Why can a lambda expression be used as a Comparator?

南笙酒味 提交于 2019-12-03 19:58:20
问题 In the book OCP Study Guide there is this example about a Comparator that can be initialized in two ways. The first is via an anonymous class like this: Comparator<Duck> byWeight = new Comparator<Duck>(){ public int compare(Duck d1, Duck d2){ return d1.getWeight() - d2.getWeight(); } }; This I can understand. According to the book this can be replaced with a lambda expression like this: Comparator<Duck> byWeight = (d1,d2) -> d1.getWeight() - d2.getWeight(); Now this I don't understand. The

Groovy - closures vs methods - the difference

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 11:37:50
If you look very carefully at the picture included, you will notice that you can refactor Groovy code using the Eclipse IDE and convert a method to a closure and vice versa. So, what exactly is a closure again and how is it different than a method? Can someone give a good example of using a closure as well as why it's a useful feature? Anonymous inner classes weren't good enough? Seagull Closure is a Closure class instance , that implements Call logic. It may be passed as argument or assigned to a variable. It also has some logic concerned with scope variable accessing and delegating calls.

Lambda behaving differently than anonymous inner class

风流意气都作罢 提交于 2019-12-03 05:40:49
问题 While doing some basic lambda exercises, the output from an apparently identical anonymous inner class was giving me a different output than the lambda. interface Supplier<T> { T get(T t); } Scenario #1 Supplier<Integer> s1 = new Supplier<Integer>() { @Override public Integer get(Integer t) { return t; } }; Supplier<Integer> s2 = t -> t; System.out.println(s1.get(2)); System.out.println(s2.get(2)); Outputs 2 and 2 . Nothing new here. But when I do this: Scenario #2 Supplier<Integer> s1 = new

Lambda behaving differently than anonymous inner class

十年热恋 提交于 2019-12-02 19:01:20
While doing some basic lambda exercises, the output from an apparently identical anonymous inner class was giving me a different output than the lambda. interface Supplier<T> { T get(T t); } Scenario #1 Supplier<Integer> s1 = new Supplier<Integer>() { @Override public Integer get(Integer t) { return t; } }; Supplier<Integer> s2 = t -> t; System.out.println(s1.get(2)); System.out.println(s2.get(2)); Outputs 2 and 2 . Nothing new here. But when I do this: Scenario #2 Supplier<Integer> s1 = new Supplier<Integer>() { @Override public Integer get(Integer t) { return t++; } }; Supplier<Integer> s2 =

Compiler is creating extra class files with $ in them

两盒软妹~` 提交于 2019-12-02 06:37:29
I'm using Eclipse and I've written a Java application using SWT. When Eclipse compiles my program, it renames my main file into 4 different files like this: MainFile.class MainFile$1.class MainFile$2.class MainFile$3.class When I go to run this program from command line, I get Could not find the main class: MainFile.class. Program will exit. I really don't understand why this is happening. Louis Wasserman The $ classes are for anonymous inner classes and perfectly normal. Could we see the command line you ran? You probably need to write java MainFile instead of java MainFile.class . 来源: https:

How can I access enclosing class instance variables from inside the anonymous class?

ⅰ亾dé卋堺 提交于 2019-12-02 01:38:12
How do I access instance variables from inside the anonymous class's method ? class Tester extends JFrame { private JButton button; private JLabel label; //..some more public Tester() { function(); // CALL FUNCTION } public void function() { Runnable r = new Runnable() { @Override public void run() { // How do I access button and label from here ? } }; new Thread(r).start(); } } How do I access instance variables from inside the anonymous class's method ? You simply access them if need be: class Tester extends JFrame { private JButton button; private JLabel label; //..some more public Tester()

Is there any way to instantiate a class defined in anonymous inner class?

蹲街弑〆低调 提交于 2019-12-01 22:14:13
问题 I was randomly writing a code & encountered a problem: how to instantiate class E (shown below) which is defined within an anonymous inner class; like: A c = new A() { class E{ //Statements } }; 回答1: You can't write a program that uses an ordinary call to new to do that: in order for a class to be instantiated, it must have a name . Anonymous inner classes, as that term implies, do not have a name. Thus a class that exists within that anonymous inner class also has no name; thus it can not be

What does the syntax mean in Java: new Stream<Integer>(){ … }?

ぐ巨炮叔叔 提交于 2019-12-01 20:34:27
I have encountered the following Java syntax that I don't recognize. This part is fine: public abstract class Stream<T> implements Iterator<T> { public boolean hasNext() { return true; } public void remove() { throw new RuntimeException("Unsupported Operation"); } } But this I don't get: Stream<Integer> ones = new Stream<Integer>() { public Integer next() { return 1; } }; while(true){ System.out.print(ones.next() + ", "); } What it is? This is providing an inline (anonymous) subclass of the Stream class. Functionally, it is the same as: public NewClass extends Stream { public Integer next() {

java, reflection , innerclass,

五迷三道 提交于 2019-12-01 20:17:40
问题 Hi i want to get the object of inner class using reflection but i am getting some error in it. code is:- package reflaction; public class MyReflection { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class obj = Class.forName("reflaction.MyReflection$TestReflection"); TestReflection a = (TestReflection) obj.newInstance(); a.demo(); } class TestReflection { public void demo(){ System.out.println("reflection occurs"); } } }