【问题标题】:non-static variable this cannot be referenced in a static context in Java非静态变量 this 不能在 Java 的静态上下文中引用
【发布时间】:2014-03-12 07:59:41
【问题描述】:

我不明白为什么这段代码会给我带来问题,因为我在外类中声明了新实例。

这是我的问题解决方案 (UvA-103):103-StackingBoxes

最初我收到 NullPointerExceptions 的运行时错误:

C:\Users\User\Desktop\103-StackingBoxes>java
Main
5 2

Exception in thread "main" java.lang.NullPointerException
        at Main.main(Main.java:27)

我已修复该问题,但现在出现以下编译错误:

C:\Users\User\Desktop\103-StackingBoxes>javac
 Main.java
Main.java:28: error: non-static variable this cannot be referenced from a static
 context
                                boxArray[i] = new box();
                                              ^
1 error

我知道在 Java 中通常会避免内部类,但我不明白为什么我的语句不起作用。

import java.util.*;
import java.io.*;
//import java.util.Arrays;

public class Main
{

    public static void main(String args[])
    {

        Scanner input = new Scanner(System.in);
        int k,n;

        while(input.hasNext())
        {
            System.out.printf("\n");
            k = input.nextInt();
            n = input.nextInt();

            // box boxArray[] = new box(n)[k];
            box[] boxArray = new box[k];

            for(int i =0; i< k; i++)
            {
                boxArray[i] = new box();
                //boxArray[i] = boxArray[i].box(n);
                boxArray[i].totalDim = n;
                for(int j =0; j < n; j++)
                {
                    boxArray[i].dimensions[j]=input.nextInt();
                }
            }


            boxArray = sortBoxArray(boxArray);

            int count = 1;
            for(int i =k-1; i > 1 ; i--)
            {
                if(boxArray[i].doesArgBoxFitInside(boxArray[i-1]))
                    count++;
                else
                    break;
            }

            System.out.printf("%d\n",count);
            for(int i = k-count; i < k ; i++)
            {
                if(i == k-1)
                    System.out.printf("%d",boxArray[i].placeNumber);
                else
                    System.out.printf("%d ",boxArray[i].placeNumber);
            }


        }

    }

    public static box[] sortBoxArray(box[] boxArray)
    {
        for(int i = 1; i < boxArray.length; i++)
        {
            for(int j = boxArray.length-1; j>=i; j++)
            {
                boolean skip = false;
                for(int k = 0; k < boxArray[j].totalDim; k++)
                {
                    if(boxArray[j].dimensions[k]<boxArray[j].dimensions[k-1])
                    {
                        box temp = boxArray[j-1];
                        boxArray[j-1] = boxArray[j];
                        boxArray[j]=temp;
                    }   
                }
            }
        }

        return boxArray;
    }


    public class box{

        /*******************************************Fields***********************************************/
        public int totalDim;
        public int dimensions[];
        //The field I forgot about
        public int placeNumber;

        /*******************************************Methods**********************************************/
        public box()
        {
            this.totalDim = -1;
            this.dimensions= new int[0];
            this.placeNumber = -1;
        }

        public box(int totalDim)
        {
            this.totalDim = totalDim;
            this.dimensions = new int[totalDim];
        }

        //public box(int totalDim, int[totalDim] dimensions)
        public box(int totalDim, int[] dimensions)
        {
            this.totalDim = totalDim;
            this.dimensions = dimensions;
            sortDim();

        }

        public void sortDim()
        {
            Arrays.sort(dimensions);        
        }

        public boolean doesArgBoxFitInside(box wop)
        {
            if(this.totalDim != wop.totalDim)
                return false;
            else
            {
                for(int i =0; i < totalDim; i++)
                {
                    if(this.dimensions[i]<wop.dimensions[i])
                        return false;
                }
                return true;
            }
        }
    }   
}

【问题讨论】:

    标签: java compiler-errors nullpointerexception inner-classes non-static


    【解决方案1】:

    您的类 box(请遵守 Java 对大写类名的编码约定!)是一个内部类,对您的静态代码不可见:

    “InnerClass 的实例只能存在于 OuterClass 的实例中,并且可以直接访问其封闭实例的方法和字段。 要实例化内部类,您必须先实例化外部类。然后,使用以下语法在外部对象中创建内部对象: OuterClass.InnerClass innerObject = outerObject.new InnerClass();" (http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html)。

    【讨论】:

    • 我很困惑我应该如何首先实例化 outerClass,因为它是主类......另外,Main.new box() 返回错误。
    • box 设为静态或将其分解到自己的类中。
    • 谢谢!这有助于解释很多!我的代码现在出现运行时错误,但我现在明白了。
    【解决方案2】:

    问题是:您的盒子类是主类的内部类,正如@Smutje 指出的那样,内部类对静态方法不可见。这样做的原因是:静态方法即使没有类的实例也可以执行,而内部类对象只有在有外部类的对象时才能存在,所以这两种说法有点矛盾。因此,内部类不能在静态方法中直接访问。

    修复:

    您可以将 Box 类设为静态,也可以在生成外部类的对象后创建 Box 类的实例。

    第二种解决方案的代码:

        Main obj = new Main();
        for(int i =0; i< k; i++)
        {
            boxArray[i] = obj.new box();
    

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 2019-03-17
      • 2011-11-30
      相关资源
      最近更新 更多