0

Why this code does not work?

HelloWorld.java:5: error: non-static method add(int,int) cannot be
referenced from a static context System.out.println(add(1,2));
  1. I know if I add static to the add method, it works, but why we have to use static? If this code was in C, it works, right?
  2. if you do not add static to add method, what are other ways that I can "test" my add method in main?

    public class HelloWorld {
    
        public static void main (String []args) { 
            System.out.println(add(1,2));
        }
    
        public int add (int x, int y) {
            return x+y;
        }
    }
    
domdomcodecode
  • 2,355
  • 4
  • 19
  • 27
  • possible duplicate of [Java: how to call non static method from main method?](http://stackoverflow.com/questions/7379915/java-how-to-call-non-static-method-from-main-method) – fvrghl Apr 23 '14 at 23:57

4 Answers4

4

You have to make the add(int, int) method static

public static int add(int x, int y) {
    return x + y;
}

Alternatively you can do

public static void main(String[] args) {
    HelloWorld test = new HelloWorld();
    System.out.println(test.add(1, 2));
}

The reason for this is because you are trying to call it from a static method. Static methods can be used without creating an instance of the object (like in the first scenario). In the second scenario, add can be non-static because we are actually creating an instance of the object, therefore can access it's non-static methods.

Tamby Kojak
  • 2,129
  • 1
  • 14
  • 25
  • 1
    This isn't an answer, OP asked WHY he had to make it static – domdomcodecode Apr 23 '14 at 23:41
  • Thanks, but I also want to know that what is Java's advantage to do this(creating an instance of object to call non-static method)? I believe in C programming, you don't have static? and I don't need to create an object to call a function? – user3566687 Apr 23 '14 at 23:54
  • You will probably benefit of reading something about object oriented programming. – Robert Balent Apr 24 '14 at 00:02
3

Yes, main method is run in static context. If you want to run method without the static keyword, you will need to instantiate the class which contains the method. Something like this:

public class HelloWorld {

    public static void main(String []args){ 

        HelloWorld helloWorld = new HelloWorld();    
        System.out.println(helloWorld.add(1,2));

    }

    public int add(int x, int y){
        return x+y;
    }
}
Robert Balent
  • 1,452
  • 11
  • 21
0

static methods don't need to be called from the context of an object; other methods do. If you want add to be non-static, try this:

public class HelloWorld {

    public static void main (String []args) { 
        HelloWorld h = new HelloWorld();
        System.out.println(h.add(1,2));
    }

    public int add (int x, int y) {
        return x+y;
    }
}

However, since HelloWorld doesn't have any data members, there really isn't any reason to do this.

user3553031
  • 5,990
  • 1
  • 20
  • 40
0

Uses of static keyword is when you want to access an Object by Class instead by instance. For example imagine you have a variable you want to be the same while the app is alive:

class example_static{
  public final static int var= 6;
}

If you want to use it, just call its class and the variable:

class example{
    public...main{
     System.out.println(String.valueOf(example_static.var));
    }
}

If you want a good example just check the Math class, alot of static methods, because would be so redundant to do something like this:

Math math = new Math(); //Eww

So with static method you get rid of it just calling the methd directly and using the Class directly as reference:

Math.methodCall(1+1);

Note

You can't call a non-static method from a static method, same with variables.

Luis Pena
  • 4,132
  • 2
  • 15
  • 23