繁体   English   中英

非静态变量,不能在Java的静态上下文中引用

[英]non-static variable this cannot be referenced in a static context in Java

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

这是我对问题(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;
            }
        }
    }   
}

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

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

问题是:您的box类是Main类的内部类,如@Smutje所指出的那样,内部类对静态方法不可见。 原因是:即使没有类的实例也可以执行静态方法,并且只有在有外部类的对象的情况下内部类对象才可以存在,因此这两种语句都是矛盾的。 因此,内部类不能在静态方法中直接访问。

固定:

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

第二种解决方案的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM