Java:访问静态方法
可以从静态方法访问非静态方法吗? 静态方法可以从非静态方法访问吗?
Can a non-static method be accessed from a static method?
And can a static method be accessed from a non-static method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态方法可以从非静态方法访问,但反之则不行。如果您位于静态方法内,则只有在有要调用该方法的对象实例时才能访问非静态方法。
A static method can be accessed from a non-static, but not the other way around. If you're inside a static method, you can only access a non-static method if you have an instance of an object on which to call the method.
可以从静态方法访问非静态方法吗?
不能,因为访问非静态方法需要对象,而访问静态方法时有可能对象不可用
可以从非静态方法访问静态方法吗?
是的。
Can a non-static method be accessed from a static method?
No because an object is required to access non-static method and there are chances that an object is not available while accessing a static method
can a static method be accessed from a non-static method?
Yes.
要访问非静态方法,您需要有一个对象的实例。如果您位于静态方法内部,那么您无法调用非静态方法,因为根据定义,您不在对象“内部”(因此您没有任何对象可以调用)调用此方法)。但是,如果您拥有对要调用非静态方法的对象的引用,则可以从静态调用非静态方法。
To access a non-static method, you need to have an instance of an object. If you are inside a static method, then you are not able to call non-static method, because, per definition, you are not "inside" an object (so you don't have any object to call this method on). You can however call non-static methods from static if you have a reference to the object you want to call the non-static method on.