ERRORnon-static variable this can not be referenced from a static context

1

I am implementing a class that allows me to create a linked list. But at the moment of instantiating a new linked list, I miss this error:

  

MainListaEnlazada.java:19: error: non-static variable this can not be referenced from a static context                   LinkedList list = new LinkedList ();

Could someone explain to me what I did wrong?

    public class MainListaEnlazada {
    public static void main(String[] args){
        ListaEnlazada lista = new ListaEnlazada();

    }

    class Nodo {
        private Object valor;
        private Nodo siguiente;

        public Nodo(Object valor){
            this.valor = valor;
            this.siguiente = null;
        }

        public void enlazarSiguiente(Nodo n) {
            this.siguiente = n;
        }

        public Object getValor(){
            return valor;
        }

        public Nodo getSiguiente() {
            return siguiente;
        }

    }

    class ListaEnlazada {
        private Nodo cabeza;
        private int size;

        ListaEnlazada(){
            cabeza = null;
            size = 0;
        }

        public void prepend(Object _element) {
            if(cabeza == null){
                cabeza = new Nodo(_element);
            } else {
                Nodo temp = cabeza;
                Nodo nuevo = new Nodo(_element);
                nuevo.enlazarSiguiente(temp);
                cabeza = nuevo;
            }

            this.size++;
        }

        public boolean vacia(){
            return (cabeza == null)?true:false;
        }

        public int size(){
            return this.size;
        }
    }
}
    
asked by Jax95 29.10.2017 в 21:43
source

1 answer

0

That's why you're using internal classes, what you have to do is write the internal classes in the following way:

public static class Nombre_Clase_Interna {

....

}

In your case it would look like this:

public class MainListaEnlazada {
public static void main(String[] args){
    ListaEnlazada lista = new ListaEnlazada();

}

static class Nodo {
    private Object valor;
    private Nodo siguiente;

    public Nodo(Object valor){
        this.valor = valor;
        this.siguiente = null;
    }

    public void enlazarSiguiente(Nodo n) {
        this.siguiente = n;
    }

    public Object getValor(){
        return valor;
    }

    public Nodo getSiguiente() {
        return siguiente;
    }

}

static class ListaEnlazada {
    private Nodo cabeza;
    private int size;

    ListaEnlazada(){
        cabeza = null;
        size = 0;
    }

    public void prepend(Object _element) {
        if(cabeza == null){
            cabeza = new Nodo(_element);
        } else {
            Nodo temp = cabeza;
            Nodo nuevo = new Nodo(_element);
            nuevo.enlazarSiguiente(temp);
            cabeza = nuevo;
        }

        this.size++;
    }

    public boolean vacia(){
        return (cabeza == null)?true:false;
    }

    public int size(){
        return this.size;
    }
    }
}

Another alternative to solve your problem is to place each class within its own file, in this case it would no longer be necessary to put static classes.

    
answered by 29.10.2017 / 21:51
source