1

I'm learning java and right now testing how static blocks work. I am trying to build and run the below code, but the static blocks are not being executed. When I compile and run the same code via the command-line (I am using command prompt(Windows 10)), the static blocks are being executed.
I am assuming this is related to some option in the IDE, but as I said I'm a still learning Java and OOP.

package statictest;

public class StaticTest {
    public static void main(String args[]) {
        System.out.println(Test.i);
    }
}

class Test {
    static int i;
    static {
        i = 10;
    }
}

Can anyone help me out? An explanation of why this happens is also much appreciated.
PS:
When using NetBeans the output is 0
When using command-line the output is 10

  • Per my reading of [the language specification](https://docs.oracle.com/javase/specs/jls/se11/html/jls-12.html#jls-12.4.1), NetBeans is doing it wrong. I don't see why there would be an "option" to not implement the language correctly, so maybe it's a bug pure and simple. – user16632363 Sep 18 '21 at 01:12
  • 1
    netbeans uses `javac` under the hood. There is surely no `--do-something-bizarre-and-break-spec` option in netbeans. There is no allowance in the lang spec here; this must print `10`. Check a few things: Did you save the file before running it, for example? – rzwitserloot Sep 18 '21 at 01:31
  • 2
    I run the code in Netbeans 12.4 using Java 16.0.2 and it worked just like your command line output. Since Netbeans is relying on the installed version of Java (and not an internal version that some IDEs use), I don't, personally, see it as a bug in Netbeans itself, but I can't answer the question of why it's happening, other then you're possibly running the wrong code or are running an out date version of the code (ie do a clean and build) – MadProgrammer Sep 18 '21 at 01:32
  • 1
    How did you create your NetBeans project? Was it created using "Java with Maven"? Or "Java with Ant"? If I create one of each type, the "with Maven" project gives `10` but the "with Ant" project gives `0` - and yes that is after a clean & build in both projects. Interestingly, the JAR file from the Ant project gives `10` when executed at the command line. (I cannot explain the differences.) – andrewJames Sep 18 '21 at 02:28
  • @andrewjames I am running a Java with Ant project, maybe that's the issue. I am positive that it's not a older version of the code. thnx for the speedy reply. Can someone explain the difference between an Ant project and a Maven project? – Isira Ratnayake Sep 18 '21 at 02:36
  • OK - so try the "with Maven" version. That would help to narrow things down to (probably) how NetBeans uses Ant. – andrewJames Sep 18 '21 at 02:40
  • "with Ant" means NetBeans uses [Ant](https://ant.apache.org/) to manage compilation and build, using its own custom `build.xml` script. "with Maven" means NetBeans uses [Maven](https://maven.apache.org/) instead. I would generally recommend Maven (or Gradle) over Ant, these days. But that's a big topic for a small comment. – andrewJames Sep 18 '21 at 02:51
  • @IsiraRatnayake FYI I used the "ant" based project for my test. The difference between Ant and Maven is a little like trying to compare apples and oranges, but they are, essentially, two different build systems – MadProgrammer Sep 18 '21 at 03:08
  • @andrewjames I rewrote the program as a maven project and worked like a charm. Although it takes a while to build and run doesn't it? – Isira Ratnayake Sep 18 '21 at 04:11
  • thannks guys for helping me out. It's also a bit strange how this was not an issue for @MadProgrammer – Isira Ratnayake Sep 18 '21 at 04:14
  • Conversely, I think that it was strange that it was an issue for you. AFAIK, nobody else managed to reproduce your problem. Certainly, nobody *said* that they had. – Stephen C Sep 18 '21 at 04:41

2 Answers2

0

i got this problem too after updating from netbeans 12.0 to 12.4, importing my old plugins fixed the problem, i think it's something related to nb-javac plugin.

Cien
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 25 '21 at 00:51
0

You should upgrade to NetBeans 12.5.

This is a known issue in NetBeans 12.4, and has been fixed in NetBeans 12.5 (I ran a test to confirm).

Summary from the NetBeans 12.5 Features page:

NETBEANS-5832 Fixing compilation of static initializer for vanilla indexing.: https://github.com/apache/netbeans/pull/3054

The specific NetBeans JIRA ticket: Static block not compiled

andrewJames
  • 19,570
  • 8
  • 19
  • 51