java 为什么从非静态方法访问静态方法不好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/405336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is accessing a static method from a non-static method bad?
提问by Jon Skeet
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:
Netbeans 告诉我从非静态方法访问静态方法很糟糕。为什么这很糟糕?“访问静态方法 getInstance”是警告:
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);
}
}
}
回答by orip
You're probably accessing the static method from an instance instead of directly. Try using Calendar.getInstance()instead:
您可能是从实例而不是直接访问静态方法。尝试使用Calendar.getInstance():
private String getSystemTime()
{
return Calendar.getInstance().get(Calendar.HOUR)+":"+
Calendar.getInstance().get(Calendar.MINUTE);
}
回答by Jon Skeet
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?
“返回静态方法”是什么意思?在我看来,从实例方法调用静态方法是可以的——当然,这取决于具体情况。你能发布一些 Netbeans 抱怨的代码吗?
One thing I couldimagine 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 looklike this code does?
编辑:对于编辑过的问题,这很有意义。IMO首先允许它是Java中的一个缺陷。它可能会产生非常误导性的代码。我最喜欢的例子(这意味着老手很可能已经看到我之前发布过:) 是Thread.sleep. 这是什么看起来像这样的代码呢?
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.sleepis 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.
在我看来,新线程似乎被要求休眠 - 类似于调用suspend. 但是不 - 您只能要求当前执行的线程休眠,这就是Thread.sleep静态方法的原因。上面的代码是合法的 Java,它将使当前正在执行的线程休眠一秒钟,而新创建的线程(可能)运行......根本不是代码乍一看的样子。
回答by Beau Simensen
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!
你的顺序颠倒了吗?如果是这样,您不能从静态方法访问非静态方法是有道理的。如果没有,我也想知道为什么这很糟糕!
回答by Janis
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;
如果您调用非静态方法,1)您使用 a=new Class() 创建新实例;2)然后调用方法a.method;
if you call static method: 1) you call it Class.method;
如果你调用静态方法:1)你称之为 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.
您可以使用静态方法来做到这一点,因为它在其类中是独立的,并且拥有调用所需的一切。如果它依赖于其他一些信息(作为构造函数),你没有将它声明为静态,它将失败。
回答by Mark
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 ?
这是 Netbeans 错误还是警告?你能发布导致它的代码吗?
回答by Ronald Blaschke
It's just fine to call time.getInstance(). The compiler will look at the type of the variable, Calendarin this case, and call the method there. It ends up being compiled exactly as Calendar.getInstance(). Note that the actual value of timedoes not contribute to this, i.e. it can even be nulland it doesn't matter.
打电话就好了time.getInstance()。Calendar在这种情况下,编译器将查看变量的类型,并在那里调用方法。它最终完全按照Calendar.getInstance(). 请注意, 的实际值time对此没有贡献,即它甚至可以null并且无关紧要。
It's this indirection and difference from regular methods that is frowned upon. It's best to express it directly as Calendar.getInstance().
正是这种间接性和与常规方法的区别令人不悦。最好直接表示为Calendar.getInstance().
回答by prashanth
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..............
java中所有的静态成员变量都会先加载到内存中,然后加载所有静态成员,然后将非静态变量和成员函数加载到内存中,然后执行静态主块...... . 所以它给出了一个非......的错误

