I am getting this error: List.java:259: non-static variable this cannot be referenced from a static context Link newl=new Link(sString);
/******************************************************
Name: List
Project number: 4
Submission date: today
Contents: List(), add(),remove(), isEmpty(), display()
pop()
******************************************************/
publicclass List
{
private Link head;
public List()
{
head=null;
}
/******************************************************
Name: Link
Project number: 4
Submission date: today
Contents: Link(), setNext(), getNext(), setPrev(),
getPrev()
******************************************************/
publicclass Link
{
Object value;
private Link next, prev;
public Link(Object v)
{
next=null;
prev=null;
this.value=v;
}
publicvoid setNext(Link n)
{
this.next=n;
}
publicvoid setPrev(Link p)
{
this.prev=p;
}
public Link getNext()
{
return next;
}
public Link getPrev()
{
return prev;
}
}
/**************************************************
Name: add()
Parameters: Link
Declared variables: Link temp
Return value: void
Algorithm:
check if there is anything in the list.
if there isn't then make head point to the specified
link.
else there is something, at least one item in the list.
check if the item that head is pointing to's next
is equal to null if it isn't then make the item that head
is pointing's next equal to the specified link.
else check if the item that head's pointing to's next is
equal to null, if not then make the specified equal to that
next.
**************************************************/
publicvoid add(Link l)
{
Link temp=head;
if(head==null)
{
head=l;
}
else
{
while(temp.getNext()!=null)
{
temp = temp.getNext();
}
temp.setNext(l);
l.setPrev(temp);
}
}
/**************************************************
Name: remove()
Parameters: int
Declared variables: Link temp
Return value: void
Algorithm:
cycle through the list checking every value of every
link in the list, when the value matches the
specified remove the links to it.
and set them.
**************************************************/
publicvoid remove(Object enteredValue)
{
Link temp=head;
if(isEmpty())
{
return;
}
elseif(temp.value.equals(enteredValue))
{
head=temp.getNext();
}
else
{
while(!temp.value.equals(enteredValue))
{
temp=temp.getNext();
}
if(temp.getNext()!=null)
{
temp.getNext().setPrev(temp.getPrev());
temp.getPrev().setNext(temp.getNext());
}
else
{
temp.getPrev().setNext(null);
}
}
}
/**************************************************
Name: isEmpty()
Parameters: none
Declared variables: none
Return value: boolean
Algorithm:none
**************************************************/
publicboolean isEmpty()
{
return head==null;
}
/**************************************************
Name: display()
Parameters: int
Declared variables: Link temp
Return value: void
Algorithm:
cycle through the list printing every value in it.
**************************************************/
publicvoid display()
{
Link temp=head;
System.out.println("--The list--");
if(isEmpty())
{
System.out.println("List is empty");
}
else
{
while(temp.getNext()!=null)
{
System.out.print(temp.value +" ");
temp = temp.getNext();
}
System.out.print(temp.value +" ");
System.out.print("null");
System.out.println();
}
}
/**************************************************
Name: pop()
Parameters: none
Declared variables: Link temp
Return value: void
Algorithm:
Cycle through to the end of the list
delete the pointers to the last item
then return the last item.
**************************************************/
publicvoid pop()
{
Link temp=head;
while(temp.getNext()!=null)
{
temp = temp.getNext();
}
if(temp.getPrev()==null)
{
head=null;
}
else
{
System.out.println("Popped: "+temp.value);
temp.getPrev().setNext(null);
}
}
/**************************************************
Name: clear()
Parameters: none
Declared variables: none
Return value: void
Algorithm:none
**************************************************/
publicvoid clear()
{
head=null;
}
/**************************************************
Name: contains()
Parameters: int
Declared variables: Link temp
Return value: boolean
Algorithm:
cycle through the list checking every value of every
link in the list, when the value matches the
specified remove the links to it.
and set them.
**************************************************/
publicboolean contains(Object v)
{
Link temp=head;
if(isEmpty())
{
System.out.println("List is empty!");
}
else
{
while(temp!=null)
{
if(temp.value.equals(v))
{
returntrue;
}
temp=temp.getNext();
}
}
returnfalse;
}
publicstaticvoid main(String[] args){
List list=new List();
String sString=new String("hello");
Link newl=new Link(sString);
list.add(newl);
}
}
someone help me out here, I am tired of looking at this code.
The problem is that you have a non-static inner class named "Link" in your class "List". In order to access a non-static inner class, even simply to instantiate it, you need a reference to an instance of the enclosing class. So one way you could do it is:
publicstaticvoid main(String[] args) {
List ist = new List();
Link ink = ist.new Link();
}
If you don't specifically require that your non-static inner class be non-static, an easier way to do this would be to make it into a nested static class:
publicclass List {
publicstaticclass Link {
...
}
...
}
The simple lesson here is that, like non-static fields and non-static methods, a non-static inner class cannot be referenced or used from a static context like public static void main.