What is a stub main method? - java

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.

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.

What are the benefits of redirecting main() method to another method on program start?

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();

Warning: main methods should not be directly called

I currently work on a Java project which has many main methods. Now, I do some refactoring, which affects all main methods. However, some of the main methods are actually called directly from other methods. I think this is bad style and it actually makes the refactoring harder. Thus, I want to be able to identify all places where a main method is called. Currently, I use my IDE tools to find all references to a given main method, however this is tedious and it is easy to forget to check this for one of the main methods.
My question is, whether there is some checkstyle rule or Eclipse compiler warnings setting that produces a warning whenever a main method is called directly. I was unable to find one.
If you are concerned that people may have formatted the "main" methods in variety of ways, and that you may miss some of them when you use "pattern matching" ...
Could "javap -public MyClass.class" work for you? When I tried it on ...
public class Main1 {
public
static
void
main
(
String
[]
args
)
{
System.out.println("Main1: Hello World");
}
}
It produced the following output:
Compiled from "Main1.java"
public class Main1 {
public Main1();
public static void main(java.lang.String[]);
}
Admittedly, it doesn't 'detect' if this 'main' is calling another 'main', it will be helpful just in finding all the 'main's.
Source derived from Post: Java Program to disassemble Java Byte Code
As #Jeutnarg suggested in the comments:
[...] you could use Eclipse's Java search. Search for 'main', only searching for 'Method' with Limit to 'References' with Search in 'Sources' with a scope of your project.
This is what I ended up doing and it actually worked much better than I expected.

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.

Categories