8

The only discernable difference between these two programs is the Java version.

The question is: what on earth is going on?

This image is proof that both programs contain exactly the same code, while producing different results.

enter image description here

here is the code:

static int x = 10;

static {
    x = x + 10;
}

public static void main(String[] args) {
    System.out.println(System.getProperty("java.version"));
    System.out.println(new Date(System.currentTimeMillis()));
    System.out.println(x);
}
  • LEFT side is using the old netbeans distribution.

  • RIGHT side is using the apache netbeans IDE.

  • LEFT side is using jdk8 (see image)

  • RIGHT side is using jdk13 (see image)

Based on the comments: jdk13 is EOL ... but some people are unable to reproduce it while using jdk13.

The question is: what's causing this?

  • netbeans distribution?
  • borked jdk
  • ... some setting in the apache IDE?
  • ... the stars being out of alignemnt?
  • etc.
user229044
  • 232,980
  • 40
  • 330
  • 338
EverNight
  • 964
  • 7
  • 16
  • 1
    It highlights the fact that it's the same code. – EverNight Jan 25 '22 at 09:43
  • 1
    On my machine, I get the same result for Java 8 and Java 16 and Java 17. The result is 20. (Which matches my understanding of what it should be.) – Stephen C Jan 25 '22 at 09:53
  • looks like a specific bug for 13.... – EverNight Jan 25 '22 at 09:54
  • Well ... maybe ... or maybe you somehow have a borked JDK installation. Either way, Java 13 is EOL. You (and anyone else reading this!) should at this point be using Java 11 or Java 17. Java 12 through 16 are all EOL. – Stephen C Jan 25 '22 at 09:56
  • 1
    I have 13.0.2 installed on my machine - can't reproduce. (It outputs 20 as expected). Maybe try to rebuild your project or something - you may be running a different piece of code. – assylias Jan 25 '22 at 10:02
  • @assylias I rebuilt it a few times.... – EverNight Jan 25 '22 at 10:03
  • I have adoptopenjdk v13.0.2+8 – assylias Jan 25 '22 at 10:04
  • 1
    Can you compile and run both programs without an IDE? is the result still the same? – Lino Jan 25 '22 at 10:10
  • 1
    Yea ... it could be that one of the IDE's is running a stale .class file ... i.e. one that doesn't match the source code that you are looking at. Note that the actual behavior here (static initialization) is implemented in the JVM itself. – Stephen C Jan 25 '22 at 10:11
  • @StephenC; @assylias ;@Lino Compilation of the source code through command line javac & java jdk 3 bundle execution yields different (and also correct) results from what the IDE executes. – EverNight Jan 25 '22 at 10:29
  • I just grabbed the `adoptopenjdk:13.0.2_8-jre-openj9-0.18.0-bionic` Docker image, which seems to match the AdoptOpenJDK version you are running. Compiled the code and ran it, and the output was **20**. Therefore, I cannot reproduce it. – MC Emperor Jan 25 '22 at 10:53
  • 1
    While we're at the subject — you shouldn't use `Date` anymore. It's outdated, archaic and antique. Also, it's [troublesome](https://stackoverflow.com/q/1969442/507738). Use classes from `java.time` instead. – MC Emperor Jan 25 '22 at 10:58

1 Answers1

1

ANSWER: Underlying problem determined to exist in IDE's compilation/build/execution routine.

Reinstall and update IDE, adoption of non-EOL JDK.

enter image description here

Also, I did not import existing IDE settings.

user229044
  • 232,980
  • 40
  • 330
  • 338
EverNight
  • 964
  • 7
  • 16
  • 1
    Seems like a bug in whatever compiler your IDE is using. The JLS is rather clear on the order of execution [JLS 12.4.1](https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4.1): "Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, **in textual order**, as though they were a single block." – Voo Jan 25 '22 at 11:24