-1

In the following code

  1. why "Node head" is kept outside the inner class node?
  2. Since Node class is defined after writing "Node head", does it create any problem?
  3. why is the inner class defined as static?

    class LinkedList{ Node head; // head of list

    /* Linked list Node.  This inner class is made static so that
       main() can access it */
    static class Node {
        int data;
        Node next;
        Node(int d)  { data = d;  next=null; } // Constructor
    }
    
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
S.P.
  • 185
  • 19

2 Answers2

2
  1. Because head is an attribute of the LinkedList class

  2. Nope, but you're welcome to move the field afterwards

  3. Java inner class and static nested class

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

1) Head is defined outside of the inner class because the inner class does not need a "Head" field, but the outer class does.

2) No, it doesn't.

3) As the comment says, it is defined as static so that main() can access it.

EKW
  • 2,059
  • 14
  • 24