anonymous-class

Are anonymous classes a subset of inner class?

随声附和 提交于 2019-12-02 13:07:23
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. 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). Anonymous classes are types of inner classes. See http://docs.oracle.com/javase/tutorial/java/javaOO

what is the activity name in anonymous class

[亡魂溺海] 提交于 2019-12-02 08:14:40
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 public void onClick(View v) { Log.d("My app","Button is pressed"); Toast.makeText(this,"Button pressed"

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()

Why are all anonymous classes implicitly final?

家住魔仙堡 提交于 2019-12-01 16:48:18
问题 According to the JLS: 15.9.5 Anonymous Class Declarations An anonymous class declaration is automatically derived from a class instance creation expression by the compiler. An anonymous class is never abstract (§8.1.1.1). An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.2). An anonymous class is always implicitly final (§8.1.1.2) . This seems like it was a specific design decision, so chances are it has some history. If I choose to have a class like this:

Is Java “caching” anonymous classes?

百般思念 提交于 2019-12-01 15:41:52
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 :) ArrayList<Integer> currentList = new ArrayList<Integer>() {{ add(i); }}; is creating a new instance of the anonymous class each time through your loop, it's not redefining or reloading the class every time

Java: Access local variables from anon inner class? (PriorityQueue)

放肆的年华 提交于 2019-12-01 10:36:38
I want to use a PriorityQueue to do a topological sort on a graph. For brevity, I'd like to use an anonymous inner class for the comparator. However, I need access to the graph g in order to determine the in degree of the nodes I'm looking at. Is this possible? /** * topological sort * @param g must be a dag */ public static Queue<String> topoSort(DirectedGraph<String, DefaultEdge> g) { Queue<String> result = new PriorityQueue<String>(g.vertexSet().size(), new Comparator<String>() { DirectedGraph<String, DefaultEdge> g; @Override public int compare(String arg0, String arg1) { if (g.inDegreeOf

Javadoc for method of anonymous object

血红的双手。 提交于 2019-12-01 08:59:29
What is the best way to properly and usefully document a function of an anonymous object? I am doing some programming with Soar (API here ), and have code that looks something like this: /** * * @return handler that does blah */ public static RhsFunctionInterface functionBlah() { return new Kernel.RhsFunctionInterface() { /** * Does blah */ @Override public String rhsFunctionHandler(int eventID, Object data, String agentName, String functionName, String arguments) { return null; } }; } When it is important to know what the function of the returned object does, what it expects for parameters,

Javadoc for method of anonymous object

此生再无相见时 提交于 2019-12-01 07:26:51
问题 What is the best way to properly and usefully document a function of an anonymous object? I am doing some programming with Soar (API here), and have code that looks something like this: /** * * @return handler that does blah */ public static RhsFunctionInterface functionBlah() { return new Kernel.RhsFunctionInterface() { /** * Does blah */ @Override public String rhsFunctionHandler(int eventID, Object data, String agentName, String functionName, String arguments) { return null; } }; } When it

Java: Should serializable inner & anonymous classes have SerialVersionUID?

末鹿安然 提交于 2019-11-30 22:25:01
Although I'm not currently planning to serialize anything, I give all serializable outer classes, as well as static nested classes a SerialVersionUID , because that is the proper way to do it. However, I've read here that Serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged for several reasons. ... So my question is: Should I give inner and anonymous classes a SerialVersionUID each, or should I add a @SuppressWarnings("serial") to those? Is one way more proper than the other? I will in any case

Singletons, Enums and anonymous inner classes

扶醉桌前 提交于 2019-11-30 19:57:08
As you may know, some people are declaring singletons with an Enum of 1 instance, because the JVM guarantees that there will always be a single instance with no concurrency problems to handle... Thus what about an Enum with multiple instances? Can we say something like an Enum is a kind of ordered set of singletons sharing a common interface? Why? public enum EnumPriceType { WITH_TAXES { @Override public float getPrice(float input) { return input*1.20f; } public String getFormattedPrice(float input) { return input*1.20f + " €"; } }, WITHOUT_TAXES { @Override public float getPrice(float input)