Search code examples
javastaticnon-static

Can a static method access and modify a non-static field in java?


Here is the code, I'm trying to use a non-static field in a static-method, but I don't know how to do it. package hombre.numbro;

public class EmployeeLinkedList {
    private EmployeeNode head;
    public void addToFront(Employee employee) {
    EmployeeNode node = new EmployeeNode(employee);
    node.setNext(head);
    head = node;
}
public void printList() {
    EmployeeNode current = head;
    System.out.print("HEAD -> ");
    while (current != null) {
        System.out.print(current);
        System.out.println();
        System.out.print(" -> ");
        current = current.getNext();
    }
    System.out.print("null");
}
public static Employee removeFromFront(Employee employee) {
    EmployeeNode removedNode = list.head;

}

}


Solution

  • The simple answer is no. You can not access a non-static field in a static method.