jls

Why does Java bind variables at compile time?

假装没事ソ 提交于 2019-11-27 18:15:51
Consider the following example code class MyClass { public String var = "base"; public void printVar() { System.out.println(var); } } class MyDerivedClass extends MyClass { public String var = "derived"; public void printVar() { System.out.println(var); } } public class Binding { public static void main(String[] args) { MyClass base = new MyClass(); MyClass derived = new MyDerivedClass(); System.out.println(base.var); System.out.println(derived.var); base.printVar(); derived.printVar(); } } it gives the following output base base base derived Method calls are resolved at runtime and the

Case sensitivity of Java class names

霸气de小男生 提交于 2019-11-27 18:06:47
If one writes two public Java classes with the same case-insensitive name in different directories then both classes are not usable at runtime. (I tested this on Windows, Mac and Linux with several versions of the HotSpot JVM. I would not be surprised if there other JVMs where they are usable simultaneously.) For example, if I create a class named a and one named A like so: // lowercase/src/testcase/a.java package testcase; public class a { public static String myCase() { return "lower"; } } // uppercase/src/testcase/A.java package testcase; public class A { public static String myCase() {

Anonymous-Inner classes showing unwanted modifier

倾然丶 夕夏残阳落幕 提交于 2019-11-27 14:55:59
To my understanding, the following code should have printed true . However, when I ran this code it is printing false . From Java docs of Anonymous Classes 15.9.5. : An anonymous class is always implicitly final public class Test { public static void main(String args[]) { Object o = new Object() { }; System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers())); } } Can some one please help me understand this behavior. Note that the wording in the JLS of that particular section has changed significantly since then. It now (JLS 11) reads: 15.9.5. Anonymous

Order of execution of parameters guarantees in Java?

吃可爱长大的小学妹 提交于 2019-11-27 13:24:05
Given the following function call in C : fooFunc( barFunc(), bazFunc() ); The order of execution of barFunc and BazFunc is not specified, so barFunc() may be called before bazFunc() or bazFunc() before barFunc() in C . Does Java specify an order of execution of function argument expressions or like C is that unspecified? Michael Easter From the Java Language Specification (on Expressions): 15.7.4 Argument Lists are Evaluated Left-to-Right In a method or constructor invocation or class instance creation expression, argument expressions may appear within the parentheses, separated by commas.

Java in operator

守給你的承諾、 提交于 2019-11-27 12:46:24
问题 For the one millionth time, I would have liked to use an IN operator in Java, similar to the IN operator in SQL. It could just be implemented as compiler syntactic sugar. So this if (value in (a, b, c)) { } else if (value in (d, e)) { } ...would really be awesome. In fact, the above is the same as the rather verbose (and not adapted for primitives) construct here: if (Arrays.asList(a, b, c).contains(value)) { } else if (Arrays.asList(d, e).contains(value)) { } Or like this for int , long and

About reference to object before object's constructor is finished

我的未来我决定 提交于 2019-11-27 11:58:46
问题 Every one of you know about this feature of JMM , that sometimes reference to object could receive value before constructor of this object is finished. In JLS7, p. 17.5 final Field Semantics we can also read: The usage model for final fields is a simple one: Set the final fields for an object in that object's constructor; and do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished . If this is followed,

What is a capture conversion in Java and can anyone give me examples?

狂风中的少年 提交于 2019-11-27 08:05:27
I've noticed JLS talks of 5.1.10 Capture Conversion , but I fail to understand what they are. Can anyone explain them to me/give examples? Buhake Sindi Capture conversion was designed to make wildcards (in generics), ? useful. Suppose we have the following class: public interface Test<T> { public void shout(T whatever); public T repeatPreviousShout(); } and somewhere on our code we have, public static void instantTest(Test<?> test) { System.out.println(test.repeatPreviousShout()); } Because test is not a raw Test and since repeatPreviousShout() in "hindsight" returns a ? , the compiler knows

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

早过忘川 提交于 2019-11-27 07:07:12
问题 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

Testing initialization safety of final fields

扶醉桌前 提交于 2019-11-27 07:06:22
I am trying to simply test out the initialization safety of final fields as guaranteed by the JLS. It is for a paper I'm writing. However, I am unable to get it to 'fail' based on my current code. Can someone tell me what I'm doing wrong, or if this is just something I have to run over and over again and then see a failure with some unlucky timing? Here is my code: public class TestClass { final int x; int y; static TestClass f; public TestClass() { x = 3; y = 4; } static void writer() { TestClass.f = new TestClass(); } static void reader() { if (TestClass.f != null) { int i = TestClass.f.x; /

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

徘徊边缘 提交于 2019-11-27 05:29:12
问题 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