views:

3298

answers:

6

Netbeans tells me it's bad to access a static method from a non static method. Why is this bad? "Accessing static method getInstance" is the warning:

import java.util.Calendar;

public class Clock
{
    // Instance fields
    private Calendar time;

    /**
     * Constructor. Starts the clock at the current operating system time
     */
    public Clock()
    {
        System.out.println(getSystemTime());
    }

    private String getSystemTime()
    {
        return this.time.getInstance().get(Calendar.HOUR)+":"+
                this.time.getInstance().get(Calendar.MINUTE);
   }

}

+9  A: 

What do you mean by "return a static method"? It's fine to call a static method from an instance method in my view - depending on the circumstances, of course. Could you post some code which Netbeans complains about?

One thing I could imagine is if you only use static methods from an instance method, without using any of the data of the instance. Sometimes that's what's required to implement an interface or override a method from a base class, but if you're not overriding anything and you're not using any instance variables, it's nice to make the method static to show that it really doesn't depend on a particular instance.

EDIT: With the edited question, this makes a lot of sense. IMO it's a deficiency in Java that allows it in the first place. It can make for very misleading code. My favourite example (which means old-timers may well have seen me post it before :) is with Thread.sleep. What does it look like this code does?

Thread t = new Thread(someRunnable);
t.start();
t.sleep(1000);

To my mind, it looks like the new thread is asked to sleep - similar to a call to suspend. But no - you can only ask the currently executing thread to sleep, which is why Thread.sleep is a static method. The above code is legal Java, and will make the currently executing thread sleep for a second while the newly created thread (probably) runs... not at all what the code looks like at first glance.

Jon Skeet
+1  A: 

Do you have the order reversed? If so, it makes sense that you cannot access a non-static method from a static method. If not, I'd like to know why this is bad as well!

Beau Simensen
The Java compiler should stop you doing this. Because static methods are by definition unrelated to any instance, there's no context in which they can invoke a non-static method, because there's no instance on which they could invoke such a method.
Andrew Swan
+17  A: 

You're probably accessing the static method from an instance instead of directly. Try using Calendar.getInstance() instead:

private String getSystemTime()
{
    return Calendar.getInstance().get(Calendar.HOUR)+":"+
           Calendar.getInstance().get(Calendar.MINUTE);
}
orip
Thanks for the answer, I've followed this up by reading up on accessing static methods.
A: 

A non-static method can not be referenced from a static context. Static methods can be referenced from a non-static context.

Is it a Netbeans error or warning ? Can you post code that is causing it ?

Mark Robinson
The problem is the converse - a warning about accessing a static method from a non-static context.
Jonathan Leffler
A: 

It's just fine to call time.getInstance(). The compiler will look at the type of the variable, Calendar in this case, and call the method there. It ends up being compiled exactly as Calendar.getInstance(). Note that the actual value of time does not contribute to this, i.e. it can even be null and it doesn't matter.

It's this indirection and difference from regular methods that is frowned upon. It's best to express it directly as Calendar.getInstance().

Ronald Blaschke
A: 

hi, In java all static member variables will be loaded into memory first, then all static members will be loaded, after that non-static variables and member functions will be loaded into memory, after that static main block will be executed....... so it was giving the error that a non..............

prashanth