Why is a static method drawn from a non-static method?

Netbeans tells me that I am not comfortable accessing the static method from a non-static method. Why is that bad? "Access to the static getInstance method" is a 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); } 

}

+4
source share
7 answers

You are probably accessing the static method from the instance, and not directly. Use Calendar.getInstance() instead:

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

What do you mean by returning a static method? In my opinion, you can call a static method an instance method, of course, depending on the circumstances. Could you post the code that Netbeans complains about?

One thing I could imagine was to use only static methods from an instance method without using any instance data. Sometimes this is what is required to implement an interface or override a method from the base class, but if you do not override anything and you do not use any instance variables, it is nice to make the method static to show that it really does not depend on a specific instance.

EDIT: With an edited question this makes a lot of sense. IMO is a flaw in Java that allows this in the first place. This can be very misleading code. My favorite example (which means that the old-timers may have seen me post it before :) with Thread.sleep . What does this code look like?

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

In my opinion, it seems that the new thread is being asked to sleep - like calling suspend . But no - you can only ask that the current executable thread is sleeping, so Thread.sleep is a static method. The above code is legal Java and will make an executable thread thread for a second, while the thread just created (probably) works ... it doesn't look at all what the code looks like at first glance.

+15
source

Is your order canceled? If so, it makes sense that you cannot access the non-stationary method from the static method. If not, I would like to know why this is also bad!

+1
source

A non-static method cannot refer to a static context. Static methods may refer to a non-static context.

Is this a Netbeans error or warning? Can you send the code calling it?

0
source

It's just fine to call time.getInstance() . In this case, the compiler will look at the type of the Calendar variable and call the method there. As a result, it compiles exactly like Calendar.getInstance() . Note that the actual time value does not contribute to this, i.e. It may even be null , and that doesn't matter.

This is this indirectness and difference from the usual methods that have been frowned upon. It is better to express it directly as Calendar.getInstance() .

0
source

why not just explain simply:

if you call a non-static method, 1) you create a new instance using = new Class (); 2) then call the a.method method;

if you call a static method: 1) you name it Class.method;

You can do this with a static method only because it is independent in its class and has everything you need to call. If it depends on any other information (as a constructor), you do not declare it static, it does not work.

0
source

In java, all static member variables will first be loaded into memory, then all static members will be loaded, after which non-static variables and member functions will be loaded into memory, after that the static main block will be executed ....... therefore it gave an error that is not ..............

-1
source

All Articles