'Is Oracle's Java tutorial outdated?
I've recently started reading Oracle's Java tutorial and I'm not sure if it is up-to-date.
In anonymous classes lesson, it is written that: "Anonymous classes also have the same restrictions as local classes with respect to their members:
- You cannot declare static initializers or member interfaces in an anonymous class.
- An anonymous class can have static members provided that they are constant variables."
I did some testing and it looks like it's not true. Using Jdoodle's Java compiler I ran:
public class MyClass {
public static void main(String args[]) {
A a = new A() {
static {
int c = 1;
}
// Or this one
//static int c = 1;
};
}
}
class A {}
It threw an error in every version except JDK 17. Is it me misinterpreting something or is Oracle not updating its tutorials? If so, why?
I did try and googled Java 17 changes but I couldn't find anything about it.
Solution 1:[1]
In the first page of this oracle Java Tutorial documentation link , you will see that this documentation is for JDK 1.8:
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oussama ZAGHDOUD |
