2

Does the main method (that Java requests you have in a class) have to be static? For example I have this code

public class Sheet {

    public static void main(String[] args) {
        myMethod();
    }

    public void myMethod() {
        System.out.println("hi there");
    }

}

This is giving me the error

cannot make a static reference to the non-static call method from main

If I'm getting it clear, any method I call from the main method must be static, and every method I call from a static method must be static.

Why does my whole class (and if we go further, my whole program) and methods have to be static? And how can I avoid this?

Holloway
  • 6,412
  • 1
  • 26
  • 33
homerun
  • 19,837
  • 15
  • 45
  • 70
  • 3
    possible duplicate of [Why is the Java main method static?](http://stackoverflow.com/questions/146576/why-is-the-java-main-method-static) – Filburt Jan 27 '15 at 15:05
  • Start at the beginning and familiarize yourself with oo. [See here](http://docs.oracle.com/javase/tutorial/java/concepts/) – cbach Jan 27 '15 at 15:05
  • @Filburt there are more concerns in this question, that's why I provided an answer rather than just closing it as a dup. – Luiggi Mendoza Jan 27 '15 at 15:11

4 Answers4

11

Not all your methods must be static, only the main entry point for your application. All the other methods can remain non-static but you will need to use a reference of the class to use them.

Here's how your code would look like:

public class Sheet {
    public static void main(String[] args) {
        Sheet sheet = new Sheet();
        sheet.myMethod();
    }

    public void myMethod(){
        System.out.println("hi there");
    }
}

The explanation for your concerns are explained here (there's no need to dup all the info here):

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
3

Your main method must be static because that is the single point of entry in your program for that running configuration.

A static method is bound to the class, hence it cannot know about single instances of that class.

You can invoke myMethod by instantiating your Sheet class:

new Sheet().myMethod();
Mena
  • 47,782
  • 11
  • 87
  • 106
  • 1
    calling big amount of methods using this way can damage the performance? in terms of time , memory use and what so ever what would be more useful , this way or the other way creating an object of its own class in the main ? – homerun Jan 27 '15 at 15:29
  • 1
    @obeyjoseph it won't matter. If your method is slow, making it `static` won't speed it up About memory, the only difference with having a single instance of your class and 0 are basically 4 bytes (and some other bytes that depend if the JVM is 32 or 64 bits). Defining a method/field `static` depends on design. Even for small applications, you may create a single instance of your main class and call the relevant methods. – Luiggi Mendoza Jan 27 '15 at 15:32
  • 1
    @obeyjoseph if you call each method using `new Sheet().myMethod();` every time, you don't gain anything and static methods would work just as well. If you call multiple methods on the same instance, you have to use non-static methods. – Holloway Jan 27 '15 at 15:36
2

Create an instance:

public class Sheet {

    public static void main(String[] args) {
        Sheet sheet = new Sheet();
        sheet.myMethod();
    }

    public void myMethod(){
        System.out.println("hi there");
    }
}
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
1

Yes the main method has to be static, Because we will not create any object for main method. And static methods can be invoked directly at the time of class loading. As they are loaded at time of class loading, we do not have to create any object for it !!

And as we know, static is mainly for memory management. All the static methods and variables can be accessed directly used the class name. Of course we can create object for static methods and variables to access them. But it is a wastage of memory.

Not all of your methods needs to be static. Depending upon your prescribed application we can use methods as static.

Sri Teja
  • 11
  • 2