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
212
votes
14 answers

Why is x == (x = y) not the same as (x = y) == x?

Consider the following example: class Quirky { public static void main(String[] args) { int x = 1; int y = 3; System.out.println(x == (x = y)); // false x = 1; // reset System.out.println((x = y) == x);…
John McClane
  • 3,498
  • 3
  • 12
  • 33
103
votes
2 answers

Effectively final vs final - Different behavior

So far I thought that effectively final and final are more or less equivalent and that the JLS would treat them similar if not identical in the actual behavior. Then I found this contrived scenario: final int a = 97; System.out.println(true ? a :…
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
92
votes
15 answers

Why no static methods in Interfaces, but static fields and inner classes OK? [pre-Java8]

There have been a few questions asked here about why you can't define static methods within interfaces, but none of them address a basic inconsistency: why can you define static fields and static inner types within an interface, but not static…
skaffman
  • 398,947
  • 96
  • 818
  • 769
76
votes
1 answer

Order of execution of parameters guarantees in Java?

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…
tpdi
  • 34,554
  • 11
  • 80
  • 120
75
votes
4 answers

If you overwrite a field in a subclass of a class, the subclass has two fields with the same name(and different type)?

I have 3 classes: public class Alpha { public Number number; } public class Beta extends Alpha { public String number; } public class Gama extends Beta { public int number; } Why does the following code compile? And, why does the test…
Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
74
votes
4 answers

Why can't we access static content via uninitialized local variable?

Take a look at below code: class Foo{ public static int x = 1; } class Bar{ public static void main(String[] args) { Foo foo; System.out.println(foo.x); // Error: Variable 'foo' might not have been initialized …
Pshemo
  • 122,468
  • 25
  • 185
  • 269
70
votes
9 answers

How to create a class literal of a known type: Class>

Take the following: public Class> getObjectType() { // what can I return here? } What class literal expression can I return from this method which will satisfy the generics and compile? List.class won't compile, and neither will…
skaffman
  • 398,947
  • 96
  • 818
  • 769
65
votes
4 answers

Why does Java bind variables at compile time?

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…
Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64
53
votes
6 answers

Is "public static final" redundant for a constant in a Java interface?

This code: interface Config { int MAX_CONN = 20; } compiled and worked as I expected. It looks like this is the same as: interface Config { public static final int MAX_CONN = 20; } Is "public static final" redundant for a constant in a…
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
49
votes
3 answers

Lambda expression and method overloading doubts

OK, so method overloading is-a-bad-thing™. Now that this has been settled, let's assume I actually want to overload a method like this: static void run(Consumer consumer) { System.out.println("consumer"); } static void…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
48
votes
4 answers

Case sensitivity of Java class names

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…
Josh Sunshine
  • 488
  • 1
  • 4
  • 13
47
votes
4 answers

Anonymous-Inner classes showing incorrect modifier

To my understanding, the following code should have printed true as output. 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 { …
T-Bag
  • 10,916
  • 3
  • 54
  • 118
41
votes
1 answer

What is the difference (or relation) between JLS, JSR and JEP?

JLS - Java Language Specification JSR - Java Specification Requests.The formal documents that describe proposed specifications and technologies for adding to the Java platform. JEP - JDK Enhancement Proposal What is the difference (or relation)…
Kanad
  • 1,008
  • 1
  • 11
  • 14
36
votes
2 answers

What does "qualified this" construct mean in java?

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…
Inquisitive
  • 7,476
  • 14
  • 50
  • 61
35
votes
9 answers

why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?

System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE); is true. I understand that integer in Java is 32 bit and can't go above 231-1, but I can't understand why adding 1 to its MAX_VALUE results in MIN_VALUE and not in some kind of…
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
1
2 3
24 25