Questions tagged [jls]

The Java Language Specification is the definitive technical reference of the Java programming language.

The Java Language Specification is the definitive technical reference of the Java programming language. It describes the language in full detail and is usually the final source of answers for questions about Java.

Questions with this tag often expect an answer that is based on the JLS and is not specific to any compiler or implementation. If possible, the relevant section of the JLS should be provided.

369 questions
19
votes
4 answers

Is 'T.super' a legal expression as per JLS?

Consider the following set of expressions: class T {{ /*1*/ Object o = T.super; // error: '.' expected /*2*/ o.toString(); }} An attempt to compile this will fail on line /*1*/ with the error: error: '.' expected o = T.super; …
charlie
  • 1,478
  • 10
  • 20
18
votes
1 answer

Is it possible to specify default value for annotation field of another annotation type?

public @interface InnerAnnotation { String value() default "hello"; } public @interface OuterAnnotation { InnerAnnotation value() default ??? } And one more case: public @interface AnotherOuterAnnotation { InnerAnnotation[] value()…
Roman
  • 64,384
  • 92
  • 238
  • 332
18
votes
3 answers

Why does returning null for a primitive work in this case?

This ugly piece of code does compile but throws NPE if s == null public static boolean isNullOrEmpty(String s) { return s != null ? s.isEmpty() : null; } while this does not (as expected): public static boolean isNullOrEmpty(String s) { …
atamanroman
  • 11,607
  • 7
  • 57
  • 81
16
votes
1 answer

Java SE 11 - New Cases of Type Conversion in Java Language Specification

JLS §5.2 of Java SE 11 contains some new type conversion cases which JLS of Java 8 doesn't have, see item 4 and item 5 in the list: Assignment contexts allow the use of one of the following: an identity conversion a widening primitive conversion a…
Frank Mi
  • 452
  • 3
  • 11
16
votes
3 answers

Is it always safe to call getClass() within the subclass constructor?

An article on classloading states that the method getClass() should not be called within a constructor because: object initialization will be complete only at the exit of the constructor code. The example they gave was: public class…
Pacerier
  • 86,231
  • 106
  • 366
  • 634
15
votes
4 answers

Java final fields: is "taint" behavior possible with the current JLS

I'm currently trying to understand this JLS section on final fields. To understand the text in the JLS better I'm also reading The Java Memory Model by Jeremy Manson (one of creators of the JMM). The paper contains the example that got me…
user15094989
15
votes
2 answers

Initialization order of final fields

Consider these two classes: public abstract class Bar { protected Bar() { System.out.println(getValue()); } protected abstract int getValue(); } public class Foo extends Bar { private final int i = 20; public Foo() { …
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
15
votes
2 answers

Ambiguous overload in Java8 - is ECJ or javac right?

I have the following class: import java.util.HashSet; import java.util.List; public class OverloadTest extends HashSet> { private static final long serialVersionUID = 1L; public OverloadTest(OverloadTest other) {} …
Ian Robertson
  • 1,312
  • 1
  • 13
  • 14
15
votes
6 answers

Why can't a class extend an enum?

I am wondering why in the Java language a class cannot extend an enum. I'm not talking about an enum extending an enum (which can't be done, since java doesn't have multiple inheritance, and that enums implicitly extend java.lang.Enum), but a class…
Unai Vivi
  • 3,073
  • 3
  • 30
  • 46
14
votes
3 answers

Why is return-type covariance enforced for hidden static methods?

This code won't compile because of the String return type of the staticMethod in Child. class Parent { static void staticMethod() { } } class Child extends Parent { static String staticMethod() { return null; } } I know…
Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57
14
votes
2 answers

Should the following code compile under Java 1.8

given the following class: public class FooTest { public static class Base { } public static class Derived extends Base { } public interface Service { void service(T value); } public abstract…
SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
13
votes
5 answers

Annotation attribute must be a class literal? Why? Constants should be fine too

Can someone explain why String and Class annotation parameters are expected differently? Why does the compiler require literals for Classes, wherby accepting constants for Strings as well? Working example with Spring's @RequestMapping: public class…
sibidiba
  • 6,270
  • 7
  • 40
  • 50
13
votes
2 answers

Java: overloaded method resolution and varargs -- confusing example

Just when I thought I understood JLS15.12 as it applied to varargs, here's this example: package com.example.test.reflect; public class MethodResolutionTest2 { public int compute(Object obj1, Object obj2) { return 42; } …
Jason S
  • 184,598
  • 164
  • 608
  • 970
13
votes
1 answer

Does the JLS require inlining of final String constants?

I ran into an issue while manipulating some bytecode, where a certain final String constant was not inlined by the java compiler (Java 8), see the example below: public class MyTest { private static final String ENABLED = "Y"; private static…
T. Neidhart
  • 6,060
  • 2
  • 15
  • 38
13
votes
1 answer

Java "fresh type variable"

What does "fresh type variable" mean in the JLS Conversions and Promotions chapter?
Ilya Lakhin
  • 1,904
  • 1
  • 16
  • 31
1 2
3
24 25