I saw in java 21 , you don't need to write public static in you main method and you can just write
public class Main{
void main(String[] args){
System.out.println("Hi");
}
}
I uninstalled java 17 and installed java 21 , added it to the environment variables but when I am running this code , I am getting this error.
Error: Main method not found in class Main, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
]
When I am writing java --version in cmd , I am getting the version of my java as 21 but still the code is not compiling
Unnamed classes is a preview feature, which means it doesn't exist unless you opt in: It's not meant for production code and is likely to break in a future release, hence why you must opt in:
javac --release 21 --enable-preview MyFile.java
java --enable-preview myFile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With