You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何静态上下文引用非静态变量this报错?附Java代码示例

Why you're getting "non-static variable this cannot be referenced from a static context"

Hey there! Let's break down exactly why this error is popping up and how you can fix it in your code.

The Root Cause

Your Number class is a non-static inner class (notice it doesn't have the static keyword). Non-static inner classes have a crucial rule: they can only exist as part of an instance of their outer class (P194Constructor1 in this case). Behind the scenes, every non-static inner class instance holds a hidden reference to an outer class instance (that's the "this" the error is talking about).

But your main method lives inside Constructor1, a static inner class. Static methods (like main) belong to the class itself, not to any instance of the class. They run in a "static context" where there's no existing instance of the outer class to tie the Number instance to. When you try to do new Number() directly in main, the JVM can't find that required outer class reference, hence the error.

Fixes You Can Apply

1. Make the Number class a static inner class

This is the simplest fix if you don't need Number to access non-static members of P194Constructor1. Just add the static keyword to the Number class definition:

package com.company;
public class P194Constructor1 {
    // Add static here
    static class Number {
        int num;
        public Number() {
            num=10;
            System.out.println("생산자 호출!");
        }
        public int getNumber() {
            return num;
        }
    }
    static class Constructor1 {
        public static void main(String[] args) {
            Number num1=new Number();
            System.out.println(num1.getNumber());
            Number num2=new Number();
            System.out.println(num2.getNumber());
        }
    }
}

Static inner classes don't require an outer class instance, so your main method can instantiate them directly without issues.

2. Create an instance of the outer class first (if you need Number to stay non-static)

If you have a reason to keep Number as a non-static inner class (e.g., it needs to access non-static fields/methods of P194Constructor1), you need to first create an instance of the outer class, then use that instance to create the Number instance:

package com.company;
public class P194Constructor1 {
    class Number {
        int num;
        public Number() {
            num=10;
            System.out.println("생산자 호출!");
        }
        public int getNumber() {
            return num;
        }
    }
    static class Constructor1 {
        public static void main(String[] args) {
            // First create an outer class instance
            P194Constructor1 outerInstance = new P194Constructor1();
            // Then use it to create the inner class instance
            Number num1 = outerInstance.new Number();
            System.out.println(num1.getNumber());
            Number num2 = outerInstance.new Number();
            System.out.println(num2.getNumber());
        }
    }
}

This explicitly provides the required outer class reference that the non-static inner class needs.

内容的提问来源于stack exchange,提问作者shin su

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:40:54