Calling mulitple classes' method inside a package - java

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.

Related

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.

Can we add a import statement to a class using Javassist

I want to call a method in different class in a different package using the lines injected through javassist. Is there any other way to call those methods directly without making them public static and call using the fully qualified classname? In here it describes about a importPackage() method. But it looks like it can be used only with classpool object where we try to obtain the required class file.
Imports are compile time only. If you want to invoke non-static methods in another class, you will need some means of acquiring the correct object instances to invoke against, such as:
Instantiating them
Some sort of object finder
Implements a shared collection that can be accessed statically that contains the objects.
One of these would need to be injected into your class so you can get the instances, and then you can wire in the actual invocation.
It might be helpful if you provided some code samples, perhaps how the code is now, and how you would like it to be.

What is going on with this class instantiation

I am trying to understand how the following code ties into two other .java files that are in the package template. In the main method are three class being instantiated? Why put them in the main method if everything is in the package template? Will these all have Driver class as a superclass? And finally are any of the words in the three classes that are being instantiated java specific words with the exception of the work "new". Thanks for any insight into this. I am trying to understand how a project fits together so that I can write a recursive algorithm to search files. Thank you
One more question I want to make sure that I understand is why is gui in the parameter for DirectoryLister....DirectoryLister(gui);??? Does it need to be there in the main class so that it can call methods from gui??
package template;
import javax.swing.*;
public class Driver
{
public static void main(String[] args)
{
GUI gui = new GUI();
DirectoryLister dl = new DirectoryLister(gui);
gui.registerModel(dl);
}
}
Lot of questions:
In the main method are three class being instantiated?
No, only two are explicitly instantiated: GUI and DirectoryLister.
Why put them in the main method if everything is in the package template?
main method is just the entry point for your program. You may or may not put everything inside main method. Just keep in mind that that's where your program will start executing.
Will these all have Driver class as a superclass?
If you are referring to GUI and DirectoryLister then the answer is NO. Not at all. They're completely independent.
Are any of the words in the three classes that are being instantiated Java specific words with the exception of the work "new"?
No. None of them.
I want to make sure that I understand is why is gui in the parameter for DirectoryLister....DirectoryLister(gui);??? Does it need to be there in the main class so that it can call methods from gui??
DirectoryLister probably expects a GUI instance in one of its constructors. You are building your dl object with gui element by calling DirectoryLister(GUI g) constructor.
--
Also, keep in mind that your question is not related to JavaME as you tagged it. It's just a plain Java question. You won't be using JavaME here since you are importing javax.swing.* which is not available for JavaME edition.
You need to learn basic java. You are instantiating only two classes.

class Main in a package?

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.

Why the main program in Java is put into a class?

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.

Categories