jls

How does the JLS specify that wildcards cannot be formally used within methods?

别说谁变了你拦得住时间么 提交于 2019-11-28 12:37:30
I've always been wondering about some weird aspect of Java generics and the use of wildcards. Let's say, I have the following API: public interface X<E> { E get(); E set(E e); } And then, let's say we declare the following methods: public class Foo { public void foo(X<?> x) { // This does not compile: x.set(x.get()); } public <T> void bar(X<T> x) { // This compiles: x.set(x.get()); } } From my "intuitive" understanding, X<?> is in fact the same as X<T> , except that the unknown <T> is formally unknown to client code. But inside of foo() , I would guess that the compiler could infer (pseudo JLS

Final fields initialization order

萝らか妹 提交于 2019-11-28 06:47:39
Here is some code that calls static method A.f() on class that is not initialized yet. Can someone explain behavior of this code in terms of JLS? class A { final static Object b = new B(); final static int S1 = 1; final static Integer S2 = 2; static void f() { System.out.println(S1); System.out.println(S2); } } class B { static { A.f(); } } public class App { public static void main( String[] args ) { A.f(); } } Output: 1 null 1 2 A.f() in App.main() triggers initialization of class A . All constant variables are initialized. The only constant variable is S1 , which now is 1 . Then, the other

Is it true that every inner class requires an enclosing instance?

回眸只為那壹抹淺笑 提交于 2019-11-28 04:54:59
The term inner class is conventionally taken to mean "a nested class which requires an enclosing instance". However, the JLS states as follows: 8.1.3. Inner Classes and Enclosing Instances [...] Inner classes include local (§14.3), anonymous (§15.9.5) and non-static member classes (§8.5). [...] An instance of an inner class whose declaration occurs in a static context has no lexically enclosing instances. Also, 15.9.5. Anonymous Class Declarations [...] An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1). And it is well-known that an anonymous class may be

Java final field compile-time constant expression

♀尐吖头ヾ 提交于 2019-11-28 04:13:47
问题 The below text is from jls http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5.3 Even then, there are a number of complications. If a final field is initialized to a compile-time constant expression (§15.28) in the field declaration, changes to the final field may not be observed, since uses of that final field are replaced at compile time with the value of the constant expression. Can anyone Please give me better explanation for the above. i couldn't understand the

Execution order of of static blocks in an Enum type w.r.t to constructor

我们两清 提交于 2019-11-28 02:54:38
问题 This is from Effective Java : // Implementing a fromString method on an enum type private static final Map<String, Operation> stringToEnum = new HashMap<String, Operation>(); static { // Initialize map from constant name to enum constant for (Operation op : values()) stringToEnum.put(op.toString(), op); } // Returns Operation for string, or null if string is invalid public static Operation fromString(String symbol) { return stringToEnum.get(symbol); } Note that the Operation constants are put

Are there any guarantees in JLS about order of execution static initialization blocks?

跟風遠走 提交于 2019-11-28 02:16:30
I wonder if it's reliable to use a construction like: private static final Map<String, String> engMessages; private static final Map<String, String> rusMessages; static { engMessages = new HashMap<String, String> () {{ put ("msgname", "value"); }}; rusMessages = new HashMap<String, String> () {{ put ("msgname", "значение"); }}; } private static Map<String, String> msgSource; static { msgSource = engMessages; } public static String msg (String msgName) { return msgSource.get (msgName); } Is there a possibility that I'll get NullPointerException because msgSource initialization block will be

Illegal static interface method call

廉价感情. 提交于 2019-11-28 00:04:00
问题 Java-8 allows define static methods inside interface, but restricts it invocation by only interface name: 9.4: An interface can declare static methods, which are invoked without reference to a particular object. E.g.: interface X { static void y() { } } ... X x = new X() {}; x.y(); causes error: error: illegal static interface method call x.y(); ^ the receiver expression should be replaced with the type qualifier 'X' Often in JLS such kind of prohibitions have an explanation. In this case I

What does “qualified this” construct mean in java?

拟墨画扇 提交于 2019-11-27 21:08:24
In Effective Java inside the item "Item 22: Favor static member classes over nonstatic" Josh Bloch says: Each instance of a nonstatic member class is implicitly associated with an enclosing instance of its containing class. Within instance methods of a nonstatic member class, you can invoke methods on the enclosing instance or obtain a reference to the enclosing instance using the qualified this construct. What does he mean by Qualified This Construct ? Without the qualifier, x() would recurse. With the qualifier, the enclosing instance's x() method is invoked instead. class Envelope { void x(

Return value of assignment operator in concurrent code

≡放荡痞女 提交于 2019-11-27 19:38:53
Given the following class: class Foo { public volatile int number; public int method1() { int ret = number = 1; return ret; } public int method2() { int ret = number = 2; return ret; } } and given multiple threads calling method1() and method2() concurrently on the same Foo instance, can a call to method1() ever return anything other than 1? The JLS 15.26 specifies: There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=(b=c), which assigns the value of c to b and then assigns the value of b to a. Ted Hopp's answer shows that

Most specific method with matches of both fixed/variable arity (varargs)

风流意气都作罢 提交于 2019-11-27 19:27:33
问题 In section 15.12.2.5 of the Java Language Specification, it talks about how to choose the most specific method in both cases of methods with fixed arity and methods of variable arity (i.e. varargs ). What I can't find in the JLS is anything about deciding between two methods where one is of fixed arity and one of variable arity however. For example: public interface SomeApi { public String getSomething(String arg); // method 1 public String getSomething(String ... args); // method 2 }