简体   繁体   中英

Does every method in main class have to be static?

I'm a total noob at java, but while practicing tonight it occurred to me that with OOP design every method in the main class is going to have to be static right? In this code there is no way for me to call a method within the class that isn't static.

It seems like maybe I'm missing the point of why you would declare a class static or not. Thanks for your help!

public class JavaApplication2 {

private static CreateCar Vroom;
private static Limo Fuuu;

public static void main(String[] args) {
     Vroom = new CreateCar();
     Vroom.creator();
     getGas();
     addGas();
     getGas();
     Fuuu = new Limo();
     Fuuu.creator();
     Fuuu.wheels = 5;
     Fuuu.wheelie();
}
 public static int getGas(){
     Vroom.returnGas();
     return 0;
 }
 public static void addGas(){
     Vroom.fillerUp();
 } 
}

You can call non-static methods, but you can only do so through an object. That is, you need to call the method on a given object.

Your main class can also be instantiated, so not every method in the main class needs to be static. For example:

public class MainClass {
    int value;

    public void printValue() {
        System.out.println("" + value);
    }

    public static void main(String[] args){
        MainClass o = new MainClass();
        o.printValue();
    }
}

No. But the main method have to be static .

To call a non-static method in the class, you have to create a reference to an object of that class . Then you call the method from the reference.

public class JavaApplication2 {
    // non-static method
    public void go() {
        System.out.println("hello");
    }

    public static void main(String[] args) {
        // declare a reference
        JavaApplication2 app;
        // create an object
        app = new JavaApplication2();
        // call a method
        app.go();
    }
}

Short answer: yes.

The reason is that your JVM needs to invoke your class'es "main" independent of already having any existing instances of that class .

Minor semantic nit-picking: "main" is a member (a static member) of a class; it isn't the name of the class itself.

Useful tip: each class can have it's OWN , DIFFERENT "main()". This can be extremely useful for unit-testing individual classes.

This sometimes comes as a shock to C/C++ programmers, where "main()" is associated with the ".exe", and you can only ever have one of them. In java, you can have many "main()" methods, and invoke whichever one you choose at runtime.

Er, you don't have a "static Main" class. You have a static method named main in your JavaApplication2 class.

There's no requirement to have any other static methods in that class. To use the class, you'd instantiate it:

public static void main(String[] args) 
{
    JavaApplication2 myApp = new JavaApplication2();
    myApp.someMethod(); 
    ...
}

main is a specifically named static method that acts as an entry point. When you tell Java to "run" your program from the command line like:

java JavaApplication2

The JVM calls JavaApplication2.main() passing in the command line arguments.

short answer: no, every method does NOT need to be static.

in fact:

  1. for small exercises, it is best to not have a different class for your main method.
  2. you can have multiple classes with main methods (pretty common in many libraries, for testing or demonstrating the use of the class)
  3. when you invoke a java program, which is typically a jar file, you have a choice of which class's main method you want to invoke
  4. as a beginner try to avoid static method unless you are sure you need them. this is because somebody not good at object oriented programming can easily abuse static methods to do function-oriented programming (not to be confused with functional programming) by using static methods.
  5. a main method (note, i am not saying "the" main method) must be static: public static void main (String [] argv) { /* ... */ }

method is declared as static, so as to be referred by class name... eg

Math.pow(x,y)  // x^y

In this method called user don't have to worry about instance creation... as Math has all its method as static so thats the basic reason why static methods are used....

static method can call non-static methods though object of class

ClassName cn =new ClassNam().methodName;

main() is special method, its starting point of execution of the program, so u can never have a object without running the program, and hence its called like(execution:: java ClassName), so it must be static...

Static methods mean that you can call it directly through

public class ClassName{
    //no-op
    public static void method(){//....}
}    
ClassName.method();

And, in some cases, the ClassName can be omitted eg.

static import

main method

But to invoke a non-static method, you must first initiate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM