anonymous-class

Are all final variables captured by anonymous classes?

余生颓废 提交于 2019-12-03 10:08:49
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 anonymous subclass will live for a while. Obviously this object has to maintain a reference to obj1 so that

Is a good practice create anonymous AsyncTask for parallel small known freeze process? [closed]

最后都变了- 提交于 2019-12-03 08:37:33
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . E.g.: you gonna do something that will take a few seconds and don't wanna freeze your UI thred, right? You could use an AsyncTask but you don't wanna create a external (or inner) class to solve a small freeze problem. So, is a good pratice do it? package com.example

Java/Android: anonymous local classes vs named classes

烂漫一生 提交于 2019-12-03 07:03:00
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 - small enough and fits good for recommendations from Java™ in a Nutshell book: In general, you should

What's the harm in using Anonymous class?

别来无恙 提交于 2019-12-03 03:34:07
问题 The question arose while reading a answer to this question - How do I join two lists in java. This answer gave the solution List<String> newList = new ArrayList<String>() { { addAll(listOne); addAll(listTwo); } }; Reading the comments, users said it was evil and ugly and should not be used in production. I would like to know what's the harm in using this? Why is it ugly, evil or bad to use in production? Note: Asked it as a question because, the post referenced is too old (2008) and the

Is it possible to serialize anonymous class without outer class?

五迷三道 提交于 2019-12-03 02:58:31
I made a small research on web and reviewed related topics on this site, but the answers were contradictory: some people said it is not possible, others said it is possible, but dangerous. The goal is to pass an object of the anonymous class as a parameter of the RMI method. Due to RMI requirements, this class must be serializable. Here's no problem, it is easy to make class Serializable. But we know that instances of inner classes hold a reference to an outer class (and anonymous classes are inner classes). Because of this, when we serialize instance of inner class, instance of outer class is

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

南笙酒味 提交于 2019-12-03 02:12:31
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 we don't want that the variable should be out-of-sync between anonymous class and local function. The

Is a good practice create anonymous AsyncTask for parallel small known freeze process? [closed]

匆匆过客 提交于 2019-12-03 00:12:13
Closed . This question is opinion-based. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . E.g.: you gonna do something that will take a few seconds and don't wanna freeze your UI thred, right? You could use an AsyncTask but you don't wanna create a external (or inner) class to solve a small freeze problem. So, is a good pratice do it? package com.example.stackoverflowsandbox; import android.os.AsyncTask; public class Foo { // E.g. before call foo method you change you

what is the activity name in anonymous class

久未见 提交于 2019-12-02 21:51:48
问题 Hello I'm new to android and I'm confused with this keyword in activity contexts. Here is a code snippet which simply prints to the screen when a button is pressed. But the studio is raising an issue. protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("My app","onCreate is called"); Toast1("onCreate"); Button btn=(Button)findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override

Can I specify a meaningful name for an anonymous class in C#?

喜欢而已 提交于 2019-12-02 20:01:17
We all know that when we create an anonymous class like this: var Employee = new { ID = 5, Name= "Prashant" }; ...at run time it will be of type: <>f__AnonymousType0<int,string> Is there any way to specify a meaningful name to such classes? Chad Grant public class Employee {} It's an anonymous type, that defeats the purpose. Those objects are designed to be temporary. Hell, the properties are even read-only. Sorry, I'm being a smart-ass. The answer is no, there is no way to tell the compiler what name to use for an anonymous type. In fact, the names of the types generated by the compiler use

What's the harm in using Anonymous class?

和自甴很熟 提交于 2019-12-02 17:03:23
The question arose while reading a answer to this question - How do I join two lists in java . This answer gave the solution List<String> newList = new ArrayList<String>() { { addAll(listOne); addAll(listTwo); } }; Reading the comments, users said it was evil and ugly and should not be used in production. I would like to know what's the harm in using this? Why is it ugly, evil or bad to use in production? Note: Asked it as a question because, the post referenced is too old (2008) and the answerer is away since several months. Natix Except for the already mentioned issues regarding good