java-topology/test/langtools/tools/javac/InnerNamedConstant_2.java
russell@unturf.com 0a580b313d undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
2026-03-26 17:11:57 -04:00

38 lines
1.2 KiB
Java

/*
* @test /nodynamiccopyright/
* @bug 4095568 4277286 4785453
* @summary Verify rejection of illegal static variables in inner classes.
* @author William Maddox (maddox)
*
* @compile/fail/ref=InnerNamedConstant_2_A.out -XDrawDiagnostics --release 15 InnerNamedConstant_2.java
* @compile/fail/ref=InnerNamedConstant_2_B.out -XDrawDiagnostics InnerNamedConstant_2.java
*/
public class InnerNamedConstant_2 {
static class Inner1 {
static int x = 1; // OK - class is top-level
static final int y = x * 5; // OK - class is top-level
static final String z; // OK - class is top-level
static {
z = "foobar";
}
}
class Inner2 {
static int x = 1; // ERROR - static not final
static final String z; // ERROR - static blank final
{
z = "foobar"; // Error may be reported here. See 4278961.
}
}
// This case must go in a separate class, as otherwise the detection
// of the error is suppressed as a result of recovery from the other
// errors.
class Inner3 {
static final int y = Inner1.x * 5; // ERROR - initializer not constant
}
}