views:

10764

answers:

9

The very common beginner mistake is when you try to use a class property "statically" without making an instance of that class. It leaves you with the mentioned error message.

You can either make the non static method static or make an instance of that class to use its properties.

Why? I am not asking for solutions. I would be grateful to know what's the reason behind it. The very core reason!

private java.util.List<String> someMethod(){
    /* Some Code */
    return someList;      
}

public static void main(String[] strArgs){          
     // The following statement causes the error. You know why..
    java.util.List<String> someList = someMethod();     
}
+6  A: 

the method you are trying to call is an instance-level method; you do not have an instance

static methods belong to the class; non-static methods belong to instances of the class

Steven A. Lowe
"non-static methods belong to instances of the class" -- The answer. But why it belongs to the instance of the class?Thank you.
ZiG
@ZiG: because you told it to by not marking it static
Steven A. Lowe
+18  A: 

You can't call something that doesn't exist. Since you haven't created an object, the non-static method doesn't exist yet. A static method (by definition) always exists.

Brian Knoblauch
"Since you haven't created an object, the non-static method doesn't exist yet."-- Thank you very much. I should have thought of it.
ZiG
Method itself does exist. Somewhere in the loaded class definition. So the answer is wrong :)
Vladimir Dyuzhev
@Vladimir, OK if you want to be picky. :) "doesn't exist in current context" :)
Brian Knoblauch
A: 

A static method relates an action to a type of object, whereas the non static method relates an action to an instance of that type of object. Typically it is a method that does something with relation to the instance.

Ex:

class Car might have a wash method, which would indicate washing a particular car, whereas a static method would apply to the type car.

Robin
Not all methods have side-effects! Doesn't have to be an action *to*, it could just as well be something that the object tells you.
Hugo
+1  A: 

if a method is not static, that "tells" the compiler that the method requires access to instance-level data in the class, (like a non-static field). This data would not be available unless an instance of the class has been created. So the compiler throws an error if you try to call the method from a static method.. If in fact the method does NOT reference any non-static member of the class, make the method static.

In Resharper, for example, just creating a non-static method that does NOT reference any static member of the class generates a warning message "This method can be made static"

Charles Bretana
A: 

The answers so far describe why, but here is a something else you might want to consider:

You can can call a method from an instantiable class by appending a method call to its constructor,

Object instance = new Constuctor().methodCall();

or

primitive name = new Constuctor().methodCall();

This is useful it you only wish to use a method of an instantiable class once within a single scope. If you are calling multiple methods from an instantiable class within a single scope, definitely create a referable instance.

_ande_turner_
+1  A: 

I just realized, I think people shouldn't be exposed to the concept of "static" very early.

Static methods should probably be the exception rather than the norm. Especially early on anyways if you want to learn OOP. (Why start with an exception to the rule?) That's very counter-pedagogical of Java, that the "first" thing you should learn is the public static void main thing. (Few real Java applications have their own main methods anyways.)

Hugo
+1  A: 

The compiler actually adds an argument to non-static methods. It adds a this pointer/reference. This is also the reason why a static method can not use this, because there is no object.

antiparagon
+1  A: 

The essence of object oriented programming is encapsulating logic together with the data it operates on.

Instance methods are the logic, instance fields are the data. Together, they form an object.

public class Foo
{
    private String foo;
    public Foo(String foo){ this.foo = foo; }
    public getFoo(){ return this.foo; }

    public static void main(String[] args){
        System.out.println( getFoo() );
    }
}

What could possibly be the result of running the above program?

Without an object, there is no instance data, and while the instance methods exist as part of the class definition, they need an object instance to provide data for them.

In theory, an instance method that does not access any instance data could work in a static context, but then there isn't really any reason for it to be an instance method. It's a language design decision to allow it anyway rather than making up an extra rule to forbid it.

Michael Borgwardt
A: 

I'm a bit of a newcomer, and I get this message often. It's not that I've misunderstood how it should be done, but Visual Studio and my inexperience combine to make the same mistake again and again. When I name a new class, Visual Studio wants that new class' name to begin with a capital letter. So far so good. When I make a new instance of that class, Visual Studio offers me a default name for it, which is the class name but beginning with a small letter. Somewhere later I refer to a property of my instance, but because the whole big-letter/small-letter issue isn't at the front of my mind, I name my instance with a big letter. I haven't noticed that I've accidentally asked for a static property of my instance's parent class, but Visual Studio still offers this error as if I have. In fact a more meaningful message might be "Watch out typing big and small letters at the beginning of your instance names." Maybe this is happening to you too.

We are talking here from Java perspective.
ZiG