anonymous-class

Serializing a list of anonymous delegates

杀马特。学长 韩版系。学妹 提交于 2019-12-05 02:39:46
问题 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

How are anonymous classes compiled in Java?

青春壹個敷衍的年華 提交于 2019-12-05 00:37:26
I've heard that Java bytecode actually doesn't support any kind of unnamed classes. How does javac translate unnamned classes to named ones? It synthesizes a name of the form EnclosingClass$n , where "n" is a counter for anonymous classes in EnclosingClass . Because using $ in identifiers is discouraged, these names should not collide with any user-specified names. 来源: https://stackoverflow.com/questions/5808492/how-are-anonymous-classes-compiled-in-java

java action listener: implements vs anonymous class

痴心易碎 提交于 2019-12-05 00:34:26
问题 I am trying to teach myself Java and had a question that I wasn't able to answer so far. In some of my reading online I have found two ways of using action listener that seem to do the same thing. But I am trying to figure out what is the advantage/disadvantage of one over the other. Is it better to use anonymous class like this: public MyClass() { ... myButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { //doSomething } }); ... } or is it best to

Why is an anonymous class in a static context valid

烈酒焚心 提交于 2019-12-04 23:43:35
I have a misunderstanding about what an anonymous class in Java is. Consider the following simple example: public static void main (String[] args) throws java.lang.Exception { B b = new B(){ }; System.out.println(b.b); } interface B{ int b = 1; } DEMO Why does the code compile? The JLS, chapt 15 says: An anonymous class is always an inner class (§8.1.3); it is never static But the JLS, chapt 8 An inner class is a nested class that is not explicitly or implicitly declared static. So the anonymous class is an inner class. But we use them in the static context. Why is it correct here? A class can

Reference to final object in anonymous class constructor a leak?

旧街凉风 提交于 2019-12-04 19:27:47
I'm passing a runnable to a service. The runnable duration may outlive the fragment / activity that passes it. I'm wondering if the following code snippet would leak the frag object by maintaining a reference inside the runnable? I realize I can move the whole line containing frag outside of the runnable (like I did with "String key = frag..."), but I'm asking simply to get an understanding of how/when an anonymous class would leak an object. I'm really not certain. The runnable only needs frag to determine a variable upon initializing the instance, and it's initialized in-line here. So in

Access outer anonymous class from inner anonymous class

房东的猫 提交于 2019-12-04 17:11:35
问题 I'm just curious. Is there a way to access parent in anonymous class that is inside another anonymous class? I make this example create a JTable subclass (anonymous class) override changeSelection and inside i create another anonymous class. MCVE: public class Test{ public static void main(String args []){ JTable table = new JTable(){ @Override public void changeSelection( final int row, final int column, final boolean toggle, final boolean extend) { SwingUtilities.invokeLater(new Runnable()

Are all final variables captured by anonymous classes?

筅森魡賤 提交于 2019-12-04 16:12:17
问题 I thought I knew the answer to this, but I can't find any confirmation after an hour or so of searching. In this code: public class Outer { // other code private void method1() { final SomeObject obj1 = new SomeObject(...); final SomeObject obj2 = new SomeObject(...); someManager.registerCallback(new SomeCallbackClass() { @Override public void onEvent() { System.out.println(obj1.getName()); } }); } } Assume that registerCallback saves its parameter somewhere, so that the object of the

Creating a dynamic anonymous types variables

巧了我就是萌 提交于 2019-12-04 12:32:33
Can I create an anonymous type variable and later on add more Properties? E.g. var x = new { Name = "Ahmed" }; and want to add Age to it? how can I do this? Another question: i saw on some blogs a type AnonymousType what is the name space for this class? here is am example http://www.codeproject.com/KB/cs/AnonymousTypesInCSharp.aspx First question - you can't. Second question - AnonymousType is the type the author of that article created. You have to download the source for his project to use that type. No, you cant. 2nd question: No such thing, it might refer to compiler generated class which

Reference of enclosing object escape through anonymous class- java

…衆ロ難τιáo~ 提交于 2019-12-04 12:08:44
问题 I am reading Java concurrency in practice and the below examples are from that. And my questions are What do they mean by this reference escape?. What will be the problem? . How does the this reference escapes from doSomething(e). public class ThisEscape { public ThisEscape(EventSource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e); } } ); } } How does this solves the problem public class SafeListener { private final EventListener

Java/Android: anonymous local classes vs named classes

你。 提交于 2019-12-04 11:17:18
问题 I would like to ask what is the good practice on using anonymous classes vs. named inner classes? I am writing an Android application, which includes many UI elements (buttons, text fields, etc). For many of them I need some kind of listeners, so in onCreate of the application I have bunch of quite small anonymous classes like: someButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { // do something... } } ); Each of of such anonymous class is 5 - 20 lines big