It is a preview feature & don't think, if it is of any use.
One will be using class names in real life, then why not to teach in the same pattern to the
students?
Below is the way one can try this while working on Java21-
Step 1 - Create a .java file with any name in some drive or any place,
let's name it 'UnnamedClass.java'. We are not giving any class name, we are just
creating a file in which we will write some Java statements.
Step 2 - Add below statements in the above file-
One will be using class names in real life, then why not to teach in the same pattern to the
students?
Below is the way one can try this while working on Java21-
Step 1 - Create a .java file with any name in some drive or any place,
let's name it 'UnnamedClass.java'. We are not giving any class name, we are just
creating a file in which we will write some Java statements.
Step 2 - Add below statements in the above file-
UnnamedClass.java
If you know Java already then it may seem weird & it is & seems it may cause confusion later, if someone is learning Java with such statements in the start.
Anyhow, you can create main() as static or instance method here but change getName() accordingly. Remember, we don't have any class here so we can't create any instance to call instance method from static method.
Anyhow, you can create main() as static or instance method here but change getName() accordingly. Remember, we don't have any class here so we can't create any instance to call instance method from static method.
Step 3 - To execute the above file with above statements, using Java21
Open 'Cmd' & go to the above file location & execute below command-
java --source 21 --enable-preview UnnamedClass.java
And you should see below result-
Open 'Cmd' & go to the above file location & execute below command-
java --source 21 --enable-preview UnnamedClass.java
And you should see below result-
To read more about this feature please check JEP445