Issue
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
Solution
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
Answered By - andrewJames
Answer Checked By - Marie Seifert (JavaFixing Admin)