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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
Mainclass needs to be in the fileMain.java. If you rename both the class and the file it will work.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
Mainclass an executable, you would write that code in a function with signaturepublic static void main(String[] args), and then execute your (compiled) class likejava Mainin the directory whereMain.classresides, though NetBeans likely provides you with a way to execute through the IDE as well.