Why is accessing a static method from a non-static method bad?
Question
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);
}
}
Solution
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);
}
OTHER TIPS
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.
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!
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 ?
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()
.
why just not explain simple:
if you call nonstatic method, 1) you create new instance with a=new Class(); 2) then call method a.method;
if you call static method: 1) you call it Class.method;
You can do it with static method just because it is independent within its class and have all it needs for calling. If it depends on some other info (as constructor) you dont declare it static, it will fail.
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..............