为什么我要获得“非静态变量”,这不能从静态上下文中引用“?
我有一个非常简单的课程,我想用作另一个类的子类。但是,当我将其代码放在父母的课程中时,我会得到:
非静态变量,这不能从静态上下文中引用
另一方面,当我将Sublass Gentest
的类代码放在“父级”类代码 - javaapp1
之外时, 不能从静态上下文中引用这一点。我没有得到这个错误。
public class JavaApp1 {
class GenTest {
@Deprecated
void oldFunction() {
System.out.println("don't use that");
}
void newFunction() {
System.out.println("That's ok.");
}
}
public static void main(String[] args) {
GenTest x = new GenTest();
x.oldFunction();
x.newFunction();
}
}
为什么会发生这种情况?
I have a very simple class which I want to use as a subclass of another one. But when I put its code in the parent's class I get :
non-static variable this cannot be referenced from a static context
On the other hand when I put the sublass GenTest
's class code outside the the "parent's" class code - JavaApp1
I do not get this error.
public class JavaApp1 {
class GenTest {
@Deprecated
void oldFunction() {
System.out.println("don't use that");
}
void newFunction() {
System.out.println("That's ok.");
}
}
public static void main(String[] args) {
GenTest x = new GenTest();
x.oldFunction();
x.newFunction();
}
}
Why is this happening ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您调用的方式不是正确的方法。由于内部类
gentest
是javaapp1
调用的正确方法的成员,它将是gentest x = new Javaapp1()。新Gentest(); new Gentest(); <<<<<<< /代码>
使用它,您的课程将正确编译。
The way you are invoking isn't the correct way to do that. Since the inner class
GenTest
is a member of theJavaApp1
the correct way to invoke it would beGenTest x = new JavaApp1().new GenTest();
Using it your class would compile correctly.
该类是嵌套的,这意味着您的嵌套类不是静态的,这意味着您必须通过主类的对象为嵌套类创建一个对象。这意味着您的PSVM应该是这样。
The class is nested which means that your nested class is not static, which means you have to create an object for the nested class through the object of the main class. what that means is your psvm should be like this.
Java在线
java
Java Online
java
您的嵌套类(顺便说一句不是 )并未标记为静态,因此它是一个需要编码类实例(javaapp1)为了构建它。
选项:
Javaapp1
中)创建一个
javaapp1
的实例为“ enclosing实例”:我个人会使用第二种方法 - Java中的嵌套类周围有一些奇数,所以我'd使用顶级类,除非您有充分的理由将其嵌套。 (最终选项特别凌乱,IMO。)
请参阅 JLS的第8.1.3节有关内部类的更多信息。
Your nested class (which isn't a subclass, by the way) isn't marked as being static, therefore it's an inner class which requires an instance of the encoding class (JavaApp1) in order to construct it.
Options:
JavaApp1
at all)Create an instance of
JavaApp1
as the "enclosing instance":Personally I'd go with the second approach - nested classes in Java have a few oddities around them, so I'd use top-level classes unless you have a good reason to make it nested. (The final option is particularly messy, IMO.)
See section 8.1.3 of the JLS for more information about inner classes.
当您从静态方法创建一个实例时,它应该是
static类Gentest
。而且,通常,嵌套类应该是静态的。
It should be
static class GenTest
, as you create an instance of it from static method.Also, in general, nested classes should be static.
类
Gentest
是一个非静态类,因此必须在javaapp1
的实例中实例化。如果您做静态类Gentest
您有工作的工作,否则需要创建一个javaapp1
的实例,并在非静态方法中创建gentest
。The class
GenTest
is a non-static class and therefore must be instantiated within an instance ofJavaApp1
. If you dostatic class GenTest
what you have work otherwise you need to create an instance ofJavaApp1
and create theGenTest
within a non-static method.塔尔(Thar)是因为gentest在Javaapp1的背景下被定义。因此,除非您有Javaapp1实例,否则您可以参考它。将其更改为
静态类
以使其工作。静态类Gentest ...
Thar's because GenTest is defined withing the context of JavaApp1. So you can refer to it unless you have an instance of JavaApp1. Change it to a
static class
for it to work.static class GenTest...
请使用
Please Use