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.
In the Android Source Code repository I found a Main.java file including this:
public static void main(String[] args) {
Main main = new Main();
main.run(args);
}
void run(String[] args) {
//CODE CONTUNIES
}
I found some articles saying creating instance of Main class is not allowed. I tried this in IntelliJ and it works. Is there any benefit of this or is it just a code beauty?
The article you posted says only that the JVM does not create an instance of Main when running the application. This is why it's a static method.
As per benefits, I do not see any except for leaving the main method as clean as possible and move some cumbersome logic in an other place.
Unless you are using Main.java as an actual objects with fields and useful methods you should use other static methods to organize your logic.
The class you posted, in fact, contains other methods besides run. They could as well have written another class. I feel it's more of a personal choice.
I'm pretty sure that the articles don't say that you can create instances of different objects in your main method!!! The articles said that it's not necessary to create an instance of a specific object to call the main method.
Let's suppose that you have the Application class where you have declared the main method. For calling the main method, you don't need to create an instance of Application like this Application app = new Application(), and after that to call the main method like this: app.main();
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.
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.
Why does the main method have to be put into a class? I understand the main ideas of OOP but I cannot get why the main program is defined within a class. Will such a class instantiated somewhere? I mean there is no code outside the class. What is a reason to define a class and never use objects of this class?
The Java Virtual Machine (JVM) has to start the application somewhere. As Java does not have a concept of “things outside of a class” the method that is called by the JVM has to be in a class. And because it is static, no instance of that class is created yet.
While it's true that java has no concept of methods outside of classes, it would be pretty easy to allow developers to skip all the broilerplate of actually writing out the class definition and simply let people write the main function directly, if they wanted too.
But back when people were developing java, people were more dogmatic about OO then they are now, and that probably played a big part. Think of it as an example of Opinionated software
It has one useful feature though, which that you can main functions to any class and launch them as programs. It's handy for testing specific features of those classes when you're working on them. If main() had to be in a separate file, and not part of a class, that would make things difficult. What if you want more then one main() function? What if you wanted to write functions to be called by main outside of the class? Would they go in a global namespace? Would multiple main() functions collide?
Not like those problems are impossible to solve, but other then a little extra typing, which can be especially annoying for newbies the current solution isn't too bad, especially since IDEs will generate the basic class structure when you create a new file.
It does simplify the design of the virtual machine. Since virtual machine already knows how to run a static method of a class then it can treat the main method just as any other static method.
If you put the main method in any other construct other than a class then the VM has to be modified to know about another construct complicating things even more.
When the Java language was designed, the notion that everything must be an object was a point of dogmatism. (though they left in a few primitive types). These days you could perhaps design a language that uses a closure -- even one outside any class -- instead.
What #Bombe said. I would add that to OO purists, the fact that the entry class is not instantiated is a misstep. The reason is the static main prevents someone from writing a family of main classes that share the same main() method written using a template method pattern.
If Java had been written to instantiate the main class and invoke the main method, users would have enjoyed the benefits of inheritance and interfaces.
As we know main is the entry point for the JVM to start. And in java there is nothing except classes and interfaces. So we have to have a main method in the class that too it should be a public class. and the main should always be public static, because it should be accessible for JVM to start and static because it starts without creating any objects
The main reason is so that multiple classes can have a main method. So a codebase can have many "entry points" and one just uses the class to specify which one is called. Also, this is inline with the OO design where (almost) everything is an object.
Main in java is a static method, therefore the class it's in doesn't need to be instantiated into an object, the class simply needs to be loaded.
That's simply how Java was designed: (almost) everything is an object, and code can only exist as part of a class.
Since the main() is static, it being called does not automatically lead to an instantiation of the class. However, it's perfectly possible (and quite common, at least in small Swing programs and Applets) to have the class that contains the main() be an otherwise normal class that is instantiated and used like any other class.