Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7567059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:34:09+00:00 2026-05-30T14:34:09+00:00

I have a question regarding the output of the following program. The output is

  • 0

I have a question regarding the output of the following program. The output is null. This is what I thought as well. Im thinking its because the methods called before display simply modify a copy of head and not head itself. Im assuming that I could get around this using a this.head= something right?

Heres the code:

public class List {
    private Node head;
    public List (){
        int max=3;
        int i;
        head=null;
        Node aNode=new Node(0);
        for (i=0; i<max; i++) {
            aNode.setNum(i);
            add (aNode);
            aNode.setNext(null);
            }
        }
    public void add(Node aNode) {
        Node temp;
        if(head==null)
            head=aNode;
        else {
            temp=head;
            while(temp.getNext()!=null)
                temp=temp.getNext();
            temp.setNext(aNode);
            }
        }
    public void display() {
        Node temp=head;
        while(temp!=null) {
            System.out.println(temp.getNext());
            temp=temp.getNext();
            }
        }
    }


public class Node {
    private int num;
    private Node next;
    public Node (int n) {num=n; next=null;}
    public int getNum() {return num;}
    public void setNum(int n) {num=n;}
    public void setNext(Node n) {next=n;}
    public Node getNext() {return next;}
    }

public class Driver {
    public static void main(String args[]) {
        List aList=new List();
        aList.display();
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:34:10+00:00Added an answer on May 30, 2026 at 2:34 pm

    Look at this code from the constructor:

    Node aNode=new Node(0);
    for (i=0; i<max; i++) {
        aNode.setNum(i);
        add (aNode);
        aNode.setNext(null);
        }
    

    You create one new node, and then keep trying to add that node to the list. You need the first line to be inside the for loop, so you create lots of nodes. After the constructor completes, your list only contains one node, with the value 3. Then later, in display():

    System.out.println(temp.getNext());
    

    You start by calling getNext() on the first node. Since there’s only one node, getNext() returns null, which is what you print out. You should replace this line with

    System.out.println(temp);
    

    The error is nothing to do with the this keyword at all. You only need this.foo (when foo is some data member of your class) to disambiguate when you have a both a member and a local variable or parameter with the same name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding the execution of the following program. #include<stdio.h> int main(void)
Following on from this question regarding calculating a member I have 2 calculated members
I have a question regarding a race condition scenario. The question: Consider the following
another noob question regarding F#. If I have the following code... let ExeC =
I have a question regarding the Java heap space. I have a program that
I have a question regarding memory allocation order. In the following code I allocate
Following on from my recent question regarding parsing XML files in Java I have
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have question regarding disabling browser caching. I have already found few solutions, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.