anonymous-class

Why is an anonymous inner class containing nothing generated from this code?

岁酱吖の 提交于 2019-11-26 04:25:11
问题 package com.test; public class OuterClass { public class InnerClass { public class InnerInnerClass { } } public class InnerClass2 { } //this class should not exist in OuterClass after dummifying private class PrivateInnerClass { private String getString() { return \"hello PrivateInnerClass\"; } } public String getStringFromPrivateInner() { return new PrivateInnerClass().getString(); } } When run through javac on the command line with Sun JVM 1.6.0_20 , this code produces 6 .class files:

Can we create an instance of an interface in Java? [duplicate]

拟墨画扇 提交于 2019-11-26 04:06:50
问题 This question already has answers here : Can we create an object of an interface? (5 answers) Closed 4 years ago . Is it possible to create an instance of an interface in Java? Somewhere I have read that using inner anonymous class we can do it as shown below: interface Test { public void wish(); } class Main { public static void main(String[] args) { Test t=new Test() { public void wish() { System.out.println(\"output: hello how r u\"); } }; t.wish(); } } cmd> javac Main.java cmd> java Main

Java lambdas 20 times slower than anonymous classes

a 夏天 提交于 2019-11-26 03:56:54
问题 I\'ve seen a lot of questions here about Java lambdas performance, but most of them go like \"Lambdas are slightly faster, but become slower when using closures\" or \"Warm-up vs execution times are different\" or other such things. However, I hit a rather strange thing here. Consider this LeetCode problem: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start

Can we create an object of an interface?

和自甴很熟 提交于 2019-11-26 03:51:23
问题 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

What is the $1 in class file names?

天大地大妈咪最大 提交于 2019-11-26 02:55:37
问题 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

Cast to Anonymous Type

旧时模样 提交于 2019-11-26 02:09:29
问题 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();

Java8 Lambdas vs Anonymous classes

霸气de小男生 提交于 2019-11-26 00:32:15
问题 Since Java8 has been recently released and its brand new lambda expressions looks to be really cool, I was wondering if this means the demise of the Anonymous classes that we were so used to. I\'ve been researching a bit about this and found some cool examples about how Lambda expressions will systematically replace those classes, such the Collection\'s sort method, which used to get an Anonymous instance of Comparator to perform the sort: Collections.sort(personList, new Comparator<Person>()

How are Anonymous inner classes used in Java?

无人久伴 提交于 2019-11-25 22:15:47
问题 What is the use of anonymous classes in Java? Can we say that usage of anonymous class is one of the advantages of Java? 回答1: By an "anonymous class", I take it you mean anonymous inner class. An anonymous inner class can come useful when making an instance of an object with certain "extras" such as overriding methods, without having to actually subclass a class. I tend to use it as a shortcut for attaching an event listener: button.addActionListener(new ActionListener() { @Override public

Why are only final variables accessible in anonymous class?

岁酱吖の 提交于 2019-11-25 21:37:38
问题 a can only be final here. Why? How can I reassign a in onClick() method without keeping it as private member? private void f(Button b, final int a){ b.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { int b = a*5; } }); } How can I return the 5 * a when it clicked? I mean, private void f(Button b, final int a){ b.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { int b = a*5; return b; // but return type is void } }); }

Java8 Lambdas vs Anonymous classes

懵懂的女人 提交于 2019-11-25 20:43:16
Since Java8 has been recently released and its brand new lambda expressions looks to be really cool, I was wondering if this means the demise of the Anonymous classes that we were so used to. I've been researching a bit about this and found some cool examples about how Lambda expressions will systematically replace those classes, such the Collection's sort method, which used to get an Anonymous instance of Comparator to perform the sort: Collections.sort(personList, new Comparator<Person>(){ public int compare(Person p1, Person p2){ return p1.firstName.compareTo(p2.firstName); } }); Now can be