---
title: "Printing out a linked list using toString using java"  
description: "Printing out a linked list using toString using java"  
author: "Anonymous User"  
published: 2013-10-14  
updated: 2013-10-14  
canonical: https://www.mindstick.com/forum/1659/printing-out-a-linked-list-using-tostring-using-java  
category: "java"  
tags: ["java"]  
reading_time: 5 minutes  

---

# Printing out a linked list using toString using java

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to learn how to print out a linked list. I have all the methods that I would need to use for the list, but I can't figure out how to display the [values](https://www.mindstick.com/forum/327/sum-textbox-values) of the nodes. [Right now](https://answers.mindstick.com/qa/36863/what-would-happen-if-gravity-became-5-percent-stronger-right-now) there is nothing in my main method because I kept getting errors trying to call non-[static methods](https://www.mindstick.com/forum/161339/static-methods-in-python) in the main. I have a [toString method](https://www.mindstick.com/forum/160331/what-is-the-tostring-method-in-javascript-and-how-can-it-be-used-for-type-conversion) that displays the contents of the list. How would I go about calling this toString to display the value of each node? Any [advice](https://yourviews.mindstick.com/view/50528/filament-advice-5-things-to-consider-before-you-buy-a-filament) will be greatly appreciated.

Here is the node class:

```
public class LinkedListNode
{
    private int data;
    private LinkedListNode next;
    public LinkedListNode(int data)
    {
        this.data = data;
        this.next = null;
    }
    public int getData()
    {
        return data;
    }
    public void setData(int d)
    {
        data = d;
    }
    public LinkedListNode getNext()
    {
        return next;
    }
    public void setNext(LinkedListNode n)
    {
        next = n;
    }
}
```

\

Here is the [LinkedList](https://www.mindstick.com/forum/159619/when-to-use-linkedlist-over-arraylist-in-c-sharp) class that contains the main and methods to manipulate the list:\

```
public class LinkedList {
    public LinkedListNode head;
    public static void main(String[] args) {
    LinkedList l = new LinkedList();
    l.insertFront(0);
    System.out.println(l.toString());
    }
    public LinkedList() {
        this.head = null;
    }
    public int removeFront(){
        if(head == null){
            System.out.println("Error - Attempting to call removeFront() on empty list");
            return 0;
        }else{
            int temp = head.getData();
            head = head.getNext();
            return temp;
        }
    }
    public void insertFront(int data){
        if(head == null){
            head = new LinkedListNode(data);
        }else{
            LinkedListNode newNode = new LinkedListNode(data);
            newNode.setNext(head);
            head = newNode;
        }
    }
    public void insertBack(int data){
        if(head == null){
            head = new LinkedListNode(data);
        }else{
            LinkedListNode newNode = new LinkedListNode(data);
            LinkedListNode current = head;
            while(current.getNext() != null){
                current = current.getNext();
            }
            current.setNext(newNode);
        }
    }
    public int removeBack(){
        if(head == null){
            System.out.println("Error - Attempting to call removeBack() on empty list");
            return 0;
        }else if (head.getNext() == null){
            int temp = head.getData();
            head = null;
            return temp;
        }else{
            LinkedListNode current = head;
            while(current.getNext().getNext() != null){
                current = current.getNext();
            }
            int temp = current.getNext().getData();
            current.setNext(null);
            return temp;
        }
    }
    public String toString(){
        String retStr = "Contents:\n";
        LinkedListNode current = head;
        while(current != null){
            retStr += current.getData() + "\n";
            current = current.getNext();
        }
        return retStr;
    }
    public LinkedListNode getHead() {
        return head;
    }
    public void setHead(LinkedListNode head) {
        this.head = head;
    }
}
```

## Replies

### Reply by Hank Greenberg

When the JVM tries to run your application, it calls your main method statically; something like this:

```
LinkedList.main();
```

That means there is no instance of your LinkedList class. In order to call your [toString](https://www.mindstick.com/interview/2259/what-is-the-difference-between-tostring-convert-tostring)() method, you can create a new instance of your LinkedList class.

So the body of your main method should be like this:

```
public static void main(String[] args){
    // creating an instance of LinkedList class
    LinkedList ll = new LinkedList();
    // adding some data to the list
    ll.insertFront(1);
    ll.insertFront(2);
    ll.insertFront(3);
    ll.insertBack(4);
    System.out.println(ll.toString());
}
```

\


---

Original Source: https://www.mindstick.com/forum/1659/printing-out-a-linked-list-using-tostring-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
