非静态变量 this 不能从静态上下文中引用 - 为什么在这里?

发布于 2024-12-17 22:30:36 字数 439 浏览 3 评论 0原文

我有一个代码:

package why;

public class Foo
{
    public class Foo1
    {
        String bar;

        public Foo1(String bar)
        {
            this.bar = bar;
        }

        public static Foo1 MYCONSTANT = new Foo(null);
    }

}

为什么我会得到“不能从静态上下文引用的非静态变量”? 我分配非静态类的实例。

为什么还要在这里?

public static Foo getMYCONSTANT()
{
    return new Foo(null, null);
}

谢谢

I have a code:

package why;

public class Foo
{
    public class Foo1
    {
        String bar;

        public Foo1(String bar)
        {
            this.bar = bar;
        }

        public static Foo1 MYCONSTANT = new Foo(null);
    }

}

Why do I get 'non-static variable this cannot be referenced from a static context'?
I allocate the instance of non-static class.

Why even here?

public static Foo getMYCONSTANT()
{
    return new Foo(null, null);
}

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

幸福不弃 2024-12-24 22:30:36

让我们看一下这个例子:

public class MainClass {

  public class NonStaticClass {

    public static NonStaticClass nonStatic = new NonStaticClass();
    //Compile error: The field nonStatic cannot be declared static; 
    //static fields can only be declared in static or top level types
    public static int i = 10;//this field also causes the same compile error
  }

}

问题是 NonStaticClass非静态。非静态内部类不能包含静态字段。

如果你想在内部类中拥有静态字段,你需要将类设置为静态。

来自java文档:

内部类

与实例方法和变量一样,内部类也有关联
及其封闭类的实例并可以直接访问该实例
对象的方法和字段。另外,因为内部类是
与实例关联,它不能定义任何静态成员
本身

有关更多信息,请查看嵌套类

Lets take a look at this example:

public class MainClass {

  public class NonStaticClass {

    public static NonStaticClass nonStatic = new NonStaticClass();
    //Compile error: The field nonStatic cannot be declared static; 
    //static fields can only be declared in static or top level types
    public static int i = 10;//this field also causes the same compile error
  }

}

The problem is that NonStaticClass is, well, not static. A non static inner class can't contain static fields.

If you want to have a static field in the inner class you need to make the class static.

From the java documentation:

Inner Classes

As with instance methods and variables, an inner class is associated
with an instance of its enclosing class and has direct access to that
object's methods and fields. Also, because an inner class is
associated with an instance, it cannot define any static members
itself
.

For more information take a look at Nested Classes

浪推晚风 2024-12-24 22:30:36

我不确定你真正的问题是什么......但这也许会有所帮助:

http://en.wikipedia.org/wiki/Singleton_pattern

Joshua Bloch 在其著作《Effective Java》第二版中声称
“单元素枚举类型是实现
singleton”[9] 对于任何支持枚举的 Java。枚举的使用是
非常容易实现并且在可序列化方面没有缺点
对象,必须通过其他方式规避。

public enum Singleton {
        INSTANCE;
}

I'm not sure what your real question is ... but perhaps this might help:

http://en.wikipedia.org/wiki/Singleton_pattern

In the second edition of his book "Effective Java" Joshua Bloch claims
that "a single-element enum type is the best way to implement a
singleton"[9] for any Java that supports enums. The use of an enum is
very easy to implement and has no drawbacks regarding serializable
objects, which have to be circumvented in the other ways.

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