Question

Netbeans is creating a class Main automatically when I create a new project.

so its in the constructor here i write the code and use all other classes?

What happens when I rename the Main class to something else. Will it still work?

Was it helpful?

Solution

It won't work, only because the name of the topmost class in a Java file must be named the same as the file itself. IE the Main class needs to be in the file Main.java. If you rename both the class and the file it will work.

so its in the constructor here i write the code and use all other classes?

It's generally bad practice to put all of your code inside the constructor. The constructor is generally used for setup, like initializing the fields of the class. You should separate your logic out into methods of the class, which can include calling methods on instances of other classes.

And if you want to make your Main class an executable, you would write that code in a function with signature public static void main(String[] args), and then execute your (compiled) class like java Main in the directory where Main.class resides, though NetBeans likely provides you with a way to execute through the IDE as well.

OTHER TIPS

You can rename class Main, important is function main ( public static ). In project configuration you can choose which class contains main function ( method ). But when you rename class then you have to rename file as well as class.

The constructor of this class is not important because main method is static so there is no instance of this class. You can create if manually if you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top