If a class has inheritance in a Java program, do I need to put main method in the superclass or subclass? Many programs put the main method in different positions. Can anybody tell me how to do that? Thanks a lot!
I think it might be best to have a simple class whose sole dedicated purpose is to contain the static main method. It simple and clear.
Your main method would then get things started by creating the initial objects from your program.
You could create a new dedicated class, let's say Launcher, with a main method, instantiate your classes there, and manage any unexpected exceptions:
public class Luncher{
public static void main(String args){
//insert argument checking logic
try{
new MyClass.executeLogic(someArguments);
}catch(Exception e){
//insert exception handling logic here
}
}
It's up to you where you'll put main method, but I'd just put it into new class with only one method, static Main(String[] args)
You would want to put a main() method in a place where you control, or drive all of the action of your program.
Well, it depends, But usually main method put in sub class. There is quite big difference between in inheritance and shadowing. But you can put it subclass or super class. Remember main method is static.
We have to put the main method inside the main class only. Then whether you make it a parent class or sub class doesn't matter, It will work fine, But the main method needs to be inside the main class else compiler will throw error.
Related
So I have already found this question but I'm having a hard time understanding the answers so sorry that this is a repeat question but when should you use a method written in a class vs a method written under the main method. If you're making a method should't you just put it in a class or are there benefits to writing a method under the main method?
public class MyProgram
{
public static void main(String[] args)
{
method();
classMethod.method2();
}
public static void method(){
System.out.println("Method under main method");
}
}
public class classMethod{
public static void method2(){
System.out.println("Method from class");
}
}
Output:
Method under main method
Method from class
They do the same thing is there a time I should use one way over the other?
I'm goin to assume that for under main method you are referring to create the method on the main class or file.
In java like any other language you can make what ever you want using only the main class but that isn't good, is going to create a really big file and is going to be really hard to maintain.
You use classes in all language to make your program more organize and easy to understand.
(If we dive into really complex program create classes, interface, etc has a lot more reasons that just organize the code, but stick with the basic)
When you need to create a class to hold methods?
That depends on you, and how you want to organize your code.
Its you are really new on Java I recommend to read a little bit on OOP, maybe give you some hints on when you have to use a class or not used.
I probably have quite a big misconception, and I've been looking around the internet and still cant find the answer to my question.
So in java OOP, suppose I have 2 classes one called Main.java, the other Something.java, and because each classes both have their own entry point in their main method.
which one should I start the program with (like to call all the
other classes in that program)?
And, if I were to use Main.java class, how do I call the whole things happening in Something.java? Like if that class was made to do its whole thing, with its own method, variables and all, and I'm just calling it all in the Main.java?
It's quite easy to understand theoretically, but in a program, not so much for me for some reason.
Well it's you who have to decide your entrypoint class. If you decide Main to be entry point class your other main method will treated as normal method like other methods. Hence, you can call that in entrypoint class.
Example below:
public class MultipleMain {
public static void main(String[] args) {
System.out.println("Hello World!!!! I must be executing");
AnotherWithMain.main(new String[]{});
}
}
class AnotherWithMain {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
If you call MultipleMain class i.e. java MultipleMain then output would be:
Hello World!!!! I must be executing
Hello World
and if you call AnotherWithMain then Hello World will be printed per above impl.
One program must have one main method to start with. Because if you ran two main method using java Your_Class then you have 2 jvm instance.
look into some tutorial how to use multiple classes in one program .
Using multiple classes in a Java program
Generally your main class will use other classes instance and static method. see the above simple example.
I just started learning java using eclipse IDE. I noticed that main method has to be static else it throws error. Because of this I have to declare many Scanner class' objects for each user-given input. Is there a way to make the main method non-static or defining main method without the static keyword in eclipse??
Is there a way to make the main method non-static or defining main method without the static keyword [...]?
No, this is part of how java works.
There is no way around it.
But it shouldn't impact your application since you can always create an instance of your main class and call another method on it:
public class X {
public static void main(String args[]) {
new X().nonStaticMain();
}
public void nonStaticMain() {
// just pretend this is your main
}
}
The main method is the first method that JVM looks for during compilation. This main method has to be executed even before the instantiation of any object of the class. so that later these instantiated objects will invoke other required methods. And hence, static will help the main to run before object instantiation. It is not possible to run the main method without the static keyword.
The answer is no.
You can have a look at these links too:
[A Closer Look at the "Hello World!" Application]
(https://docs.oracle.com/javase/tutorial/getStarted/application/index.html)
Why is the Java main method static?
I have just started learning Java and going through Heard up series. I see few examples where a class is created and has main in it and inside main, an object of the class is created. As main () is also part of the same class, is this ok? When you create a new Object, does it also create multiple copies of main () ? How does compiler decode this?
Sample code snippet explaining the problem
public class Hobbits {
int size;
public static void main(String [] args) {
Hobbits h = new Hobbits[3];
// This is my point of concern.
// Here i create array of objects but each Object would have main class?
}
}
The main-method is a static method. Static methods do not reference single objects, think of them as methods of a class instead of an object - they do not need any objects to work. Read this for further explanations.
"but each Object would have main class?"
Your question is not so clear, but if you are asking if every class needs to have a main() method, then the short answer is NO. You can have a single main() in a separate class that calls all your other classes (Hobbit class, Wizard class, Elf class, etc.).
If your function/variable is static, it means there is only one instance of it, does not matter how many instances of your class you will create.
second thing, it will be more aesthetic, if you create separate class only for your main method
Let's say I have Main class, and it has an instance of class A.
How can I call a method in Main class within class A ?
Thanks!
This is called Composition...Where a class has a Reference of other class...
Composition is preferred over Inheritance when we need one or few functionalities But Not all the functionalities of a class.
Eg:
public class A{
Main m = new M(); // m is a Object Reference Variable of type Main in class A
m.go(); // go() is a method in class Main
}
If it is an instance method, then you need an instance of M inside A to call M's method inside A. If it is a static method, you can just call it directly. But you're holding circular references so beware.
Main.methodName() for a static method.
Though it sounds to me like what you're trying to do may be bad practice. Your Main method or class should simply be an entry point
To call a method in Main class from calss A you need an instance of Main class within the class A(considering them in same package) if both the calsses have no relation like inheritance.if static then you may call Main.methodName();
If the method is a static method (i.e. was declared with "public static ReturnType methodName()") then in the A class, you need to call Main.methodName().
However, if the method is an instance method (declared as "public ReturnType methodName()") then you need to somehow pass an instance of Main into the instance of A (perhaps through the constructor, or a setter method). Inside the A class, you could then call instanceOfMain.methodName().
However (as some people have already mentioned) this is probably not the best way to handle things. The Main class should simply be where the program starts; it is not where you should be doing any real program logic.