Please bear with me as I've only been learning programming/Java for a couple weeks now, so please excuse me if I use any term incorrectly (or if the question is stupid...)
I've been recently learning about anonymous classes and initializer blocks, which led me to learn that before JDK16 you could not have any static declarations inside inner classes except for constants when trying to do this:
//Declare and instantiate anonymous class
static ExtendedByAnonymous anonymousObject = new ExtendedByAnonymous() {
//Static Initialization Block
//Static declarations in inner classes are not supported at language level '15'
static {
System.out.println("anonymous object created");
}```
Initially I thought this made sense, since how would you define the behaviour of a field that is static when the class that owns depends on the instance of an outer class? How would you initialize these fields? At the same time as every other static field? Whenever a new instance of the outer class was created? Seemed like it made sense to avoid these ambiguities, but then the Java developers obviously did decide on SOME way to handle this, since it is now allowed.
So my questions are:
1) Do the ambiguities I suggested actually exist? Or did I just misunderstand the core concepts of OOP?
2) If they do exist, how did the Java team solve them? Mainly, when are the static fields in inner classes initialized, and who "owns" those fields?
I'm willing to experiment more with this and maybe investigate the bytecode if nobody has a ready answer to the questions and anyone is willing to give me some guidance, as this seems to just a tad above my paygrade 😄
