why are Static blocks not being executed in the NetBeans IDE (Java)?

256 views Asked by At

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

2

There are 2 answers

0
andrewJames On BEST ANSWER

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

1
Cien On

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.