anonymous-class

Write a custom syntax interpreter in java?

删除回忆录丶 提交于 2019-12-04 09:32:55
I am about to begin coding a demo program for a lecture I'm about to give. I want to let each student in the class download this application, then be able to create instances of objects (and their graphical representations) interactively via a command line. I decided to write in java, not because it's the language I'm most familiar with, but because it's got easy graphics classes and I can be pretty sure that the jar is going to run on their computers. Intro over. Now the question: What is a good way to implement some custom command line syntax for this program? I want to use a simple,

Why can an anonymous class access non-final class member of the enclosing class

倖福魔咒の 提交于 2019-12-04 09:12:47
问题 We know that only final local variables can be accessed in an anonymous class, and there is a good reason here: Why are only final variables accessible in anonymous class?. However, I found that an anonymous class can still access a non-final variable if the variable is an member field of the enclosing class: How can I access enclosing class instance variables from inside the anonymous class? I am confused. We ensure that only a final local variable can be accessed in anonymous class because

In Jython, can I make an inline anonymous class that implements a Java interface?

早过忘川 提交于 2019-12-04 08:24:26
In Java, I can say Thread t = new Thread(){ public void run(){ // do stuff } } (or something much like that) to declare an anonymous class inline. This is most useful for making event handlers or other callback sorts of things, that in any sensible language wouldn't require an object to own them in the first place. I face the same problem in Jython -- I want to define an event handler, but I don't want to build a whole standalone class to do so. Ideally, I'd just be able to pass a lambda and be done with it, but if this is possible, I can't find it anywhere in the docs. davidrmcharles Here are

Are anonymous classes a subset of inner class?

拜拜、爱过 提交于 2019-12-04 06:59:58
问题 It might sound like a dumb question, but all anonymous classes must be defined and instantiated within an existing class; therefore, they must be inner classes at the same time. 回答1: This is quite true. Your anonymus class could not be implemented outside of other classes, as a seperate class, because as it is anonymus you would not be even able to refer to it in any way. Additional information: From JLS: An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1

Final field and anonymous class

妖精的绣舞 提交于 2019-12-04 05:33:31
I'm still not satisfied with explanation regarding anonymous class and final field. There were tons of questions trying to explain obvious problem but I have not found answers for all my questions :-) Suppose following code: public void method(final int i, int j) { final int z = 6; final int x = j; int k = 5; new Runnable() { public void run() { System.out.print(i); System.out.print(x); System.out.print(z); System.out.print(k); } }; } It's not possible to compile this code because of "unfinal" k property. I understand that compiler can replace z property with the declared value during compile

Private inner class synthesizes unexpected anonymous class

一个人想着一个人 提交于 2019-12-04 05:29:31
When you compile a Java class with a private inner class, it appears that an anonymous class is automatically synthesized along with it for some reason. This class is sufficient to reproduce it: public class SynthesizeAnonymous { public static void method() { new InnerClass(); } private static class InnerClass {} } When compiled, this generates the expected SynthesizeAnonymous.class and SynthesizeAnonymous$InnerClass.class files, but it also generates a strange SynthesizeAnonymous$1.class file that corresponds to an anonymous subclass of java.lang.Object that was synthesized. If you look at

Value of “this” in an anonymous class vs a lambda expression

心不动则不痛 提交于 2019-12-04 03:43:35
I am little bit confused with the different behavior of an anonymous class and a lambda expression. When I'm using a lambda expression: //Test.java Runnable r1 = () -> System.out.println(this); Runnable r2 = () -> System.out.println(toString()); @Override public String toString() { return "Hello World!"; } // in main method new Test().r1.run(); new Test().r2.run(); Output : Hello World! Hello World! When using an anonymous class: Runnable r1 = new Runnable() { @Override public void run() { System.out.println(this); } }; Runnable r2 = new Runnable() { @Override public void run() { System.out

Is Java “caching” anonymous classes?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 02:59:35
问题 Consider the following code: for(int i = 0;i < 200;i++) { ArrayList<Integer> currentList = new ArrayList<Integer>() {{ add(i); }}; // do something with currentList } How will Java treat the class of currentList ? Will it consider it a different class for each of the 200 objects? Will it be a performance hit even after the first object is created? Is it caching it somehow? I'm just curious :) 回答1: ArrayList<Integer> currentList = new ArrayList<Integer>() {{ add(i); }}; is creating a new

Serializing a list of anonymous delegates

你说的曾经没有我的故事 提交于 2019-12-03 17:37:53
This question may be very similar to my one, but I cannot see the answer I need in it. I have a class, called CASM , that has a List<Action> . I want to serialize this class (using the BinaryFormatter or something similar). This class and all classes referenced in the Action s have got correct [Serializable] and [NonSerializable] attributes. The problem comes when serialization is attempted - it gives this error: Type 'CASM.CASM+<>c__DisplayClass2c' in Assembly 'CASM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. This <>c__DisplayClass2c is an

Can anonymous class be used as return types in C++?

风格不统一 提交于 2019-12-03 11:37:54
Is there any way to use anonymous classes in C++ as return types? I googled that this may work: struct Test {} * fun() { } But this piece of code doesn't compile, the error message is: new types may not be defined in a return type Actually the code doesn't make any sense, I just want to figure out whether an anonymous class can be used as return type in C++. Here is my code: #include <typeinfo> #include <iterator> #include <iostream> #include <fstream> #include <cstring> #include <cstdlib> using namespace std; int main(int argc, char **argv) { int mx = [] () -> struct { int x, y ; } { return {