class Main in a package? - java

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?

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.

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.

Related

Calling mulitple classes' method inside a package

I am writing a Minecraft mod. I've got multiple classes inside a package, which all have the public static register() method. Upon starting of the Minecraft Client, I am calling every classes' register() method.
There are two problems with my code:
There are a lot of classes inside that package and having every single class inside the main class looks awful
Adding new class inside the package does not automatically add the register()-part inside the main class and can be easily forgotten.
The question is now: is there any way to automatically call every classes' register() method inside the main class?
One of my ideas was to fetch every class inside the package using ClassLoader.getSystemClassLoader().getResourceAsStream and then Class.forName, which looks like an overkill to me.
I have never actually learned java and only got to know it through examples, so don't go too hard on me.
You could create your own annotation #ToRegister and use it in your classes:
#ToRegister
public class ToRegister1 {
}
Then use Reflections to find the classes as explained in this answer.

How to execute a .java file if it has more than one class in it?

Consider I am having Sample.java file with class A and class B with two main methods respectively.
how to execute this file
A java file can not have two classes, unless one is inside the other.
In that case, the full name of the inner class will be something like somepackage.MainClass.InnerClass
You'll have to give a main class to the java program, so you'll end up choosing the main or the inner class.
That said, having a main in an inner class would be strange, to say the least.
A valid Java file is only allowed to have one public class and it must have the same name as the filename. Therefore open your cmd and run:
javac myclass.java //Compile
java myclass arg1 arg2... //Execute
A .java file can have other classes, but they aren't allowed to be public, so the execute command is not ambigious.

Extract static methods from main() file in java

I'm quite new to Java and wrote a little to-do list project.
At the beginning of the project I added a bunch of static methods to the file where my main() code is, and it got a little out of hand. I want to transfer these methods to another file.
Is there a proper way to do this, or do I just have to create some sort of Behaviour class for these methods, and then in main() create an instance of it to call it's methods?
You can extract these methods to a separate class (say FooUtils), and then in your main method you can call them using the class name - FooUtils.someStaticMethod()
Depending on what you have, it may make sense to group your methods into different classes, or to make them instance methods.

What is a stub main method?

I have an assignment in my Object Oriented Programming class and I'm having a little confusion with the Project Setup.
So I created a new java application project called "BMIOption". and Within this project, I had to create a package that contains two java classes, "Bmioption" and "BmiRecord".
It says to add a stub main method to the "Bmioption" class file and edit the project properties to make "Bmioption" as the main class to run.(I'm assuming instead of the having the "BMIOption" the main class to run).
Can someone explain what a stub main method does and how to implement it? I hope I was clear enough on my confusion.
I expect that it means add the following code to the class:
public static void main(String[] args) {}
It is a stub because it doesn't really do anything, but it allows you to use the class as the entry point to your application. You can subsequently add statements to your code which add useful behaviour.
It simply means create a main method in Bmioption class.
if you are using eclipse type main and ctrl+space bar will help you generate it.

Java compilation of a .java file without a public class

Okay, so a java source file must have at least one public class and the file should be called "class-name.java". Fair enough.
Hence, if I have a class, then the following would compile:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
But what bugs me is that if I remove the 'public' access modifier from the above code, the code still compiles. I just don't get it. Removing it, the code looks like:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
In the above code, since I removed the public access modifier, my class has default or package access, i.e. it can't be accessed from the outside world, only from within the package.
So my question is, how does the above code compile ? The file HelloWorld.java in this case does not have a public HelloWorld class (only a package-private HelloWorld.class) and thus to my understanding should not compile.
a java source file must have at least one public class and the file should be called class-name.java
Incorrect, a top level class does not have to be declared public. The JLS states;
If a top level class or interface type is not declared public, then it may be accessed only from within the package in which it is declared.
See http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#104285 section 6.6.1.
You can place non-public class in a file, and it's not a bug but feature.
Your problem is on level of packaging, not compile. Because you can compile this file with non-public class, but you can't call it from outside, so it's not working as application base class
Like this:
// [+] single file: SomeWrapper.java
public class SomeWrapper {
ArrayList<_PrivateDataType> pdt;
}
// [-] single file: SomeWrapper.java
// [+] single file: _PrivateDataType.java
class _PrivateDataType {
// members, functions, whatever goes here
}
// [-] single file: _PrivateDataType.java
A main method is just like any other method. The only difference is that it may be invoked from the command line with the java command. Even if the main method is not visible from the command line, the class can still be used like any other Java class, and your main method may be invoked by another class in the same package. Therefore i makes sense that it compiles.
In Java main function are not special in any sense. There just exists a terminal command that is able to invoke static methods called main...
There are valid usages for a non public classes. So the compiler does not give error when you try to compile the file.
That's nothing to wonder about. I suppose this behavior is similar to the one of some C/C++-compiler.
Code like "void main() { /.../ }" will be compiled correctly by those compilers, although it is not standards-compliant code. Simply said, the compiler exchanges the "void" with "int".
I think a similar behavior is implemented by the java compiler.
When you do not specify the access modifier of the class (or its field or method), it is assigned "default" access. This means it is only accessible from within the same package (in this case, the default package).
The website Javabeginner.com has an article on the subject - you should become familiar with access modifiers in Java, either from this site, or others.

Categories