【问题标题】:Java error: non-static method cannot be referenced from a static contextJava 错误:不能从静态上下文中引用非静态方法
【发布时间】:2015-10-15 03:48:33
【问题描述】:

我知道对于像我这样的新手来说这是一个相当常见的错误,但我似乎无法阅读任何我能理解的内容。对于我的作业,我们需要创建一个名为 NumberList 的 long 数组。我们需要完成的方法之一是调用toString,这显然是把数组变成了字符串来打印。

  1. 因此,在我的主要方法中,我只是创建了一个空白 NumberList 对象并尝试在其上运行 toString() 方法。编译器说“不能从静态上下文引用非静态方法 toString()”。为什么!?
  2. 其次,我创建了一个要传递给我的 NumberList 对象的 long 测试数组。我调用了数组 testExample 并创建了一些任意值。但是当我写

new NumberList(testExample);

我得到更多错误。为什么我不能这样做?

最后这是我的代码。请忽略除 main、构造函数和 toString 之外的所有方法。

非常感谢

public class NumberList implements java.util.Collection {

//instance stuff
private long[] longArray;

public static void main ( String[] args ) {

    long[] testExample;
    testExample = new long[3];     
    testExample[0] = 1;
    testExample[1] = 2;
    testExample[2] = 3;

    new NumberList();
    //System.out.println(
    NumberList.toString();
    //);

}


/** Constructs an empty number list. */
public NumberList(){
    longArray = new long[0];     
    //System.out.println(longArray.length);
}


/** Constructs a number list from an array of Longs. */
public NumberList( Long[] l ){
    int size = l.length;
    longArray = new long[size];
    for (int i = 0; i < size; i++) {
        longArray[i] = l[i];
    }
}

 /** This returns a stringy version of this number list. */
public String toString () {
    //System.out.println(this.length());
    return "why doesnt this work :(";
}   

/** Increases by one the number of instances of the given element in this collection. */
public boolean add ( Object obj ) {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    //return true if the element CAN be added
    throw new UnsupportedOperationException();
}


/** Adds all of the elements of the given number list to this one. */
public boolean addAll ( java.util.Collection c  ) {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}


/** Removes all of the elements from this collection. */
public void clear () {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}


/** Returns true iff this number list contains at least one instance of the specified element. */
public boolean contains ( Object obj ) {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    //
    throw new UnsupportedOperationException();
}



/** Returns true iff this number list contains at least one instance of each element 
    in the specified list. Multiple copies of some element in the argument do not
    require multiple copies in this number list. */
public boolean containsAll ( java.util.Collection c ) {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}




/** Compares the specified object with this collection for equality. */
public boolean equals ( Object obj ) {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}




/** Returns the hashcode value for this collection. */
public int hashCode () {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    //return integer that represents the set uniquely
    //return hashcode based on the numbers in the array
    //hashCode should be equal in equal cases
    throw new UnsupportedOperationException();
}



/** Returns true if this collection contains no elements. */
public boolean isEmpty () {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}



/** Returns an iterator over the elements in this collection. Replicated elements should
    be "iterated over" just once. */
public java.util.Iterator iterator () {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}



/** Removes a single instance of the specified element from 
    this collection, if it is present. */
public boolean remove ( Object obj ) {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}



/** Removes all of this collection's elements that are also contained 
    in the specified collection. */
public boolean removeAll ( java.util.Collection c ) {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}




/** Retains only the elements in this collection that are contained in the specified collection. 
     In other words, removes from this collection all of its elements that are not contained in the 
     specified collection. */
public boolean retainAll ( java.util.Collection c ) {
    throw new UnsupportedOperationException();
}


/** Returns the number of elements in this number list, including duplicates. */
public int sizeIncludingDuplicates () {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}



/** Returns a Long[] containing all of the elements in this collection, not including duplicates. */
public Long[] toArray () {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}



/** Not supported for this class. */
public Object[] toArray ( Object[] obj ) {
    throw new UnsupportedOperationException();
}




/** Returns the number of elements in this number list, not including duplicates. */
public int size () {
    System.out.println(longArray.length);
    return 0 ;
}




/** Returns the number of instances of the given element in this number list. */
public int count ( Object obj ) {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}



/** This so-called "static factory" returns a new number list comprised of the numbers in the specified array.
    Note that the given array is long[], not Long[]. */
public static NumberList fromArray ( long[] l ) {
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */
    throw new UnsupportedOperationException();
}

}

【问题讨论】:

  • NumberList.toString(); - toString 不是静态方法,需要类的实例才能调用,例如new NumberList().toString()
  • “为什么!?”你对static了解多少?您需要在NumberList... 的特定实例 上调用toString()... 否则没有任何意义。我建议您回到您的 Java 书籍/教程/其他内容,找到关于 static 成员和实例成员的部分。
  • 接下来,请每个帖子一个问题 - 您的构造函数问题(看起来是 Long[]long[] 不兼容的问题)是完全独立的。

标签: java arrays static


【解决方案1】:

它不是静态方法,NumberList 上的 toString 只能在对象 NumberList 的实例上调用。

你必须把它当作

//create a instance of nList
NumberList nList = new NumberList();
//call the method on the newly created instance of NumberList
nList.toString();

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 2013-04-05
    • 2016-01-29
    • 1970-01-01
    • 2014-05-03
    • 2021-03-01
    • 1970-01-01
    相关资源
    最近更新 更多