arrays - Java error: non-static method cannot be referenced from a static context -
i know common error noobs myself, can't seem read can understand. homework need create array of longs called numberlist. 1 of methods need complete called tostring, turns array string print.
- so in main method created blank numberlist object , tried running tostring() method on it. compiler says "non-static method tostring() cannot referenced static context". why!?
- secondly, created test array of longs want pass numberlist object. called array testexample , made arbitrary values. when write
new numberlist(testexample);
i more errors. why can not this?
finally here's code. please ignore every method except main, constructors, , tostring.
thank much
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 empty number list. */ public numberlist(){ longarray = new long[0]; //system.out.println(longarray.length); } /** constructs number list array of longs. */ public numberlist( long[] l ){ int size = l.length; longarray = new long[size]; (int = 0; < size; i++) { longarray[i] = l[i]; } } /** returns stringy version of number list. */ public string tostring () { //system.out.println(this.length()); return "why doesnt work :("; } /** increases 1 number of instances of given element in collection. */ public boolean add ( object obj ) { /* replace next statement code */ //return true if element can added throw new unsupportedoperationexception(); } /** adds of elements of given number list one. */ public boolean addall ( java.util.collection c ) { /* replace next statement code */ throw new unsupportedoperationexception(); } /** removes of elements collection. */ public void clear () { /* replace next statement code */ throw new unsupportedoperationexception(); } /** returns true iff number list contains @ least 1 instance of specified element. */ public boolean contains ( object obj ) { /* replace next statement code */ // throw new unsupportedoperationexception(); } /** returns true iff number list contains @ least 1 instance of each element in specified list. multiple copies of element in argument not require multiple copies in number list. */ public boolean containsall ( java.util.collection c ) { /* replace next statement code */ throw new unsupportedoperationexception(); } /** compares specified object collection equality. */ public boolean equals ( object obj ) { /* replace next statement code */ throw new unsupportedoperationexception(); } /** returns hashcode value collection. */ public int hashcode () { /* replace next statement code */ //return integer represents set uniquely //return hashcode based on numbers in array //hashcode should equal in equal cases throw new unsupportedoperationexception(); } /** returns true if collection contains no elements. */ public boolean isempty () { /* replace next statement code */ throw new unsupportedoperationexception(); } /** returns iterator on elements in collection. replicated elements should "iterated over" once. */ public java.util.iterator iterator () { /* replace next statement code */ throw new unsupportedoperationexception(); } /** removes single instance of specified element collection, if present. */ public boolean remove ( object obj ) { /* replace next statement code */ throw new unsupportedoperationexception(); } /** removes of collection's elements contained in specified collection. */ public boolean removeall ( java.util.collection c ) { /* replace next statement code */ throw new unsupportedoperationexception(); } /** retains elements in collection contained in specified collection. in other words, removes collection of elements not contained in specified collection. */ public boolean retainall ( java.util.collection c ) { throw new unsupportedoperationexception(); } /** returns number of elements in number list, including duplicates. */ public int sizeincludingduplicates () { /* replace next statement code */ throw new unsupportedoperationexception(); } /** returns long[] containing of elements in collection, not including duplicates. */ public long[] toarray () { /* replace next statement code */ throw new unsupportedoperationexception(); } /** not supported class. */ public object[] toarray ( object[] obj ) { throw new unsupportedoperationexception(); } /** returns number of elements in number list, not including duplicates. */ public int size () { system.out.println(longarray.length); return 0 ; } /** returns number of instances of given element in number list. */ public int count ( object obj ) { /* replace next statement code */ throw new unsupportedoperationexception(); } /** so-called "static factory" returns new number list comprised of numbers in specified array. note given array long[], not long[]. */ public static numberlist fromarray ( long[] l ) { /* replace next statement code */ throw new unsupportedoperationexception(); }
}
it's not static method, tostring on numberlist can called on instance of object numberlist.
you must use
//create instance of nlist numberlist nlist = new numberlist(); //call method on newly created instance of numberlist nlist.tostring();
Comments
Post a Comment