anonymous-class

Java reflection: How can I retrieve anonymous inner classes?

泄露秘密 提交于 2019-11-26 14:53:14
问题 I have an anonymous inner class inside another class ( SomeClass ). Both SomeClass.class.getClasses() and SomeClass.class.getDeclaredClasses() return empty arrays. I couldn't find some hints on this in Class ' Javadocs. Can anonymous inner classes be retrieved using reflection in some way? What else are notable differences between anonymous inner classes and normal inner classes? 回答1: If it's using reflection, it's probably a really bad idea. Leaving that aside, I believe you can additional

Can we create an object of an interface?

我只是一个虾纸丫 提交于 2019-11-26 14:16:29
interface TestA { String toString(); } public class Test { public static void main(String[] args) { System.out.println(new TestA() { public String toString() { return "test"; } }); } } What is the result? A. test B. null C. An exception is thrown at runtime . D. Compilation fails because of an error in line 1. E. Compilation fails because of an error in line 4. F. Compilation fails because of an error in line 5. What is the answer of this question and why? I have one more query regarding this question. In line 4 we are creating an object of A. Is it possible to create an object of an interface

Java anonymous class efficiency implications

放肆的年华 提交于 2019-11-26 14:11:57
问题 Is there any difference in efficiency (e.g. execution time, code size, etc.) between these two ways of doing things? Below are contrived examples that create objects and do nothing, but my actual scenarios may be creating new Threads, Listeners, etc. Assume the following pieces of code happen in a loop so that it might make a difference. Using anonymous objects: void doSomething() { for (/* Assume some loop */) { final Object obj1, obj2; // some free variables IWorker anonymousWorker = new

What is the $1 in class file names?

谁说胖子不能爱 提交于 2019-11-26 12:18:02
C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>dir Volume in drive C has no label. Volume Serial Number is 2041-64E7 Directory of C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet 2009-07-02 23:54 . 2009-07-02 23:54 .. 2004-09-06 14:57 582 WelcomeApplet.html 2004-09-06 15:04 1,402 WelcomeApplet.java 2 File(s) 1,984 bytes 2 Dir(s) 2,557,210,624 bytes free C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>javac WelcomeApplet.java C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>dir Volume in drive C has no label. Volume Serial

Java 8 Lambda Expressions - what about multiple methods in nested class

*爱你&永不变心* 提交于 2019-11-26 12:17:07
I'm reading about the new features at: http://www.javaworld.com/article/2078836/java-se/love-and-hate-for-java-8.html I saw the example below: Using Anonymous Class: button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.out.println("Action Detected"); } }); With Lambda: button.addActionListener(e -> { System.out.println("Action Detected"); }); What would someone do with a MouseListener if they wanted to implement multiple methods within the anonymous class, e.g.: public void mousePressed(MouseEvent e) { saySomething("Mouse pressed; # of clicks: "

NotSerializableException on anonymous class

谁说我不能喝 提交于 2019-11-26 11:35:34
问题 I have an interface for filtering items: public interface KeyValFilter extends Serializable { public static final long serialVersionUID = 7069537470113689475L; public boolean acceptKey(String iKey, Iterable<String> iValues); public boolean acceptValue(String iKey, String value); } and a class containing a member of type KeyValFilter . public class KeyValFilterCollector extends KeyValCollectorSkeleton { private static final long serialVersionUID = -3364382369044221888L; KeyValFilter filter;

Cast to Anonymous Type

∥☆過路亽.° 提交于 2019-11-26 10:29:40
I had the following problem today, and I was wondering if there is a solution for my problem. My idea was to build anonymous classes and use it as a datasource for a WinForm BindingSource: public void Init() { var option1 = new { Id = TemplateAction.Update, Option = "Update the Templates", Description = "Bla bla 1." }; var option2 = new { Id = TemplateAction.Download, Option = "Download the Templates", Description = "Bla bla 2." }; var list = new[] {option1, option2}.ToList(); bsOptions.DataSource = list; // my BindingSource // cboTemplates is a ComboBox cboTemplates.DataSource = bsOptions;

Setting outer variable from anonymous inner class

∥☆過路亽.° 提交于 2019-11-26 08:13:07
问题 Is there any way to access caller-scoped variables from an anonymous inner class in Java? Here\'s the sample code to understand what I need: public Long getNumber(final String type, final String refNumber, final Long year) throws ServiceException { Long result = null; try { Session session = PersistenceHelper.getSession(); session.doWork(new Work() { public void execute(Connection conn) throws SQLException { CallableStatement st = conn.prepareCall(\"{ CALL PACKAGE.procedure(?, ?, ?, ?) }\");

How can an anonymous class use “extends” or “implements”?

自闭症网瘾萝莉.ら 提交于 2019-11-26 06:20:27
问题 How can an anonymous class extend a superclass or implement an interface? 回答1: Anonymous classes must extend or implement something, like any other Java class, even if it's just java.lang.Object . For example: Runnable r = new Runnable() { public void run() { ... } }; Here, r is an object of an anonymous class which implements Runnable . An anonymous class can extend another class using the same syntax: SomeClass x = new SomeClass() { ... }; What you can't do is implement more than one

How to pass parameters to anonymous class?

半世苍凉 提交于 2019-11-26 05:57:12
问题 Is it possible to pass parameters, or access external parameters to an anonymous class? For example: int myVariable = 1; myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // How would one access myVariable here? } }); Is there any way for the listener to access myVariable or be passed myVariable without creating the listener as an actual named class? 回答1: Technically, no, because anonymous classes can't have constructors. However, classes can