为什么我要获得“非静态变量”,这不能从静态上下文中引用“?

发布于 2025-02-06 15:10:54 字数 660 浏览 3 评论 0原文

我有一个非常简单的课程,我想用作另一个类的子类。但是,当我将其代码放在父母的课程中时,我会得到:

非静态变量,这不能从静态上下文中引用

另一方面,当我将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

耳根太软 2025-02-13 15:10:55

您调用的方式不是正确的方法。由于内部类gentestjavaapp1调用的正确方法的成员,它将是

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 the JavaApp1 the correct way to invoke it would be

GenTest x = new JavaApp1().new GenTest();

Using it your class would compile correctly.

情话难免假 2025-02-13 15:10:55

该类是嵌套的,这意味着您的嵌套类不是静态的,这意味着您必须通过主类的对象为嵌套类创建一个对象。这意味着您的PSVM应该是这样。

public static void main(String[] args) {
    JavaApp1 a=new JavaApp1(); //create an object for the main class
    JavaApp1.GenTest x=a.new GenTest();

    x.oldFunction();
    x.newFunction();
}

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.

public static void main(String[] args) {
    JavaApp1 a=new JavaApp1(); //create an object for the main class
    JavaApp1.GenTest x=a.new GenTest();

    x.oldFunction();
    x.newFunction();
}
音盲 2025-02-13 15:10:55
class Test {

    static class GenTest { // nested class with static

        static void oldFunction() { // static method
            System.out.println("don't use that");
        }
        void newFunction() { // non-static method
            System.out.println("That's ok.");
        }
    }

    public static void main (String[] args) {
        GenTest.oldFunction(); // call static method

        GenTest two = new GenTest(); // call non-static method
        two.newFunction();
    }

}

Java在线
java

class Test {

    static class GenTest { // nested class with static

        static void oldFunction() { // static method
            System.out.println("don't use that");
        }
        void newFunction() { // non-static method
            System.out.println("That's ok.");
        }
    }

    public static void main (String[] args) {
        GenTest.oldFunction(); // call static method

        GenTest two = new GenTest(); // call non-static method
        two.newFunction();
    }

}

Java Online
java

箹锭⒈辈孓 2025-02-13 15:10:54

您的嵌套类(顺便说一句不是 )并未标记为静态,因此它是一个需要编码类实例(javaapp1)为了构建它。

选项:

  • 使嵌套的类静态
  • 使其不是内部类(即完全不在Javaapp1中)
  • 创建一个javaapp1的实例为“ enclosing实例”:

      gentest x = new Javaapp1()。新gentest();
     

我个人会使用第二种方法 - 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:

  • Make the nested class static
  • Make it not an inner class (i.e. not within JavaApp1 at all)
  • Create an instance of JavaApp1 as the "enclosing instance":

    GenTest x = new JavaApp1().new GenTest();
    

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.

墨小沫ゞ 2025-02-13 15:10:54

当您从静态方法创建一个实例时,它应该是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.

时间你老了 2025-02-13 15:10:54

Gentest是一个非静态类,因此必须在javaapp1的实例中实例化。如果您做静态类Gentest您有工作的工作,否则需要创建一个javaapp1的实例,并在非静态方法中创建gentest

The class GenTest is a non-static class and therefore must be instantiated within an instance of JavaApp1. If you do static class GenTest what you have work otherwise you need to create an instance of JavaApp1 and create the GenTest within a non-static method.

你在看孤独的风景 2025-02-13 15:10:54

塔尔(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...

蓝海 2025-02-13 15:10:54

请使用

static class GenTest()......

Please Use

static class GenTest()......
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文