Linked List - part 2. We make the inner class public (strange but true)



public class LinkedListPublic
{
    public class Node
    {
        private String name;
        private Node next;
       
        public Node(String s)
        {
            name=new String(s);
            next=null;
            doNothingInside();    //this is OK          <----- Note: we are calling a method defined inside the inner class from inside the inner class (OK)
            doNothingOutside();  //this is OK         <----- Note: we are calling a method defined in the outer class from inside the inner class (OK)
        }
       
        public Node getNext()
        { return next;}
       
        public void setNext(Node n)
        {
            next=n;
        }
       
        public String toString()
        {
            return name;
        }
       
       
        public void doNothingInside()
        {
            System.out.println("This is inside and does nothing");
        }
       
    } //end of Node declaration
   
    private Node front;
   
   
   
    public LinkedListPublic()
    {
        front=null;
        //front.doNothingInside();      //Note- this is OK syntactically, but throws an error. Calling a method defined in the inner class from the outer class
        //front.doNothingOutside();   //This is commented because it gives an error . Calling a method in the outer class from the outer class
    }
   
    public void insertFront(String s)
    {
        Node newNode=new Node(s);
        newNode.setNext(front);
        front=newNode;
    }
   
    public void printTheList()
    {
        printForward(front);
    }
   
   
    private void printForward(Node x)
    {
        if (x!=null)
            { System.out.println(x);
              printForward(x.getNext());
            }
    }
   
    private void doNothingOutside()
    {
        System.out.println("this does nothing");
    }
   
}// end of class definition

----------------------------------------------------------
And now we use it:

public class UseLinkedListPublic
{
   public static void main(String [] args)
   {
       LinkedListPublic myList=new LinkedListPublic();                 
       LinkedListPublic.Node myNode=myList.new Node("harry");    <---- strange syntax. Must create an associated outer class first!
       LinkedListPublic.Node yourNode=myList.new Node("sue");
      
       myList.insertFront("joe");
       myList.insertFront("karen");
       myList.printTheList();    <------- those nodes we created above have nothing to do with the linked list!!
      
   }   
}