Ryoko Nela
Ryoko Nela

Reputation: 57

Why a list give me a value, yet the other doesnt?

public class INode
{
    private int value;
    private INode right, down;
    private int row, col;

    public INode(int value)
    {
        this.value = value;
    }

    public int getValue() 
    {
        return value;
    }

    public void setValue(int value) 
    {
        this.value = value;
    }

    public INode getRight() 
    {
        return right;
    }

    public void setRight(INode right) 
    {
        this.right = right;
    }

    public INode getDown() 
    {
        return down;
    }

    public void setDown(INode down)
    {
        this.down = down;
    }

    public int getRow() 
    {
        return row;
    }

    public void setRow(int row) 
    {
        this.row = row;
    }

    public int getCol() 
    {
        return col;
    }

    public void setCol(int col) 
    {
        this.col = col;
    }


}

I can get value of a = 8 but for head, even though I use constructor to set up, still give me value = null... dont know why.

And the driver is:

import java.util.*;
public class List
{
public static INode head;
public List()
{
    head = new INode(8);

}
public static void main (String[] args)
{
    INode a = new INode(8);
    int data = a.getValue();
    System.out.println(data);
    System.out.println(head.getValue());
}


}

Please help me a hand guys. Dont understand why when I use a constructor, I cant assign the value to the node, yet when I create an instance, i can...

Thank guys, love you folks! Great help!

Upvotes: 0

Views: 88

Answers (4)

Sanket
Sanket

Reputation: 393

You can do this either way .. either you can create an instance of Class List or you can initialize head to point to object of INode. This depends on the business logic that you require

public static void main(String[] args) {
    INode a = new INode(8);
    int data = a.getValue();
    System.out.println(data);
    List list = new List();
    System.out.println(list.head.getValue());

    head = new INode(6);
    System.out.println(head.getValue());
}

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213261

You should initialize your static variables either at the point of declaration, or in a static initializer block. And not in a constructor.

Constructor will only be used, when you instantiate your List class, that you are not doing anywhere. static initializer block is executed, when the class is loaded into memoty. So, your INode will get initialized when the class is loaded.

public static INode head;
static {
    head = new INode(8);
}

or just: -

public static INode head = new INode(8);
  • static variables are common to all instances. If a change is made for one instance, it will be reflected in all the instances. So be sure before using them, that you actually want that. If possible, declare your INode as non-static variable. And instantiate your List class before using it.

    public INode head = new INode(8);
    

And then in your main method, you can access it like this: -

List list = new List();
System.out.println(list.head.getValue());

Upvotes: 0

home
home

Reputation: 12538

You do not instantiate the class List. Change your code to

public INode head; // remove static
public List() {
    head = new INode(8);
}

And modify your main method:

public static void main (String[] args) {
    INode a = new INode(8);
    int data = a.getValue();
    System.out.println(data);

    List l = new List(); // create new List instance
    System.out.println(l.head.getValue()); // get the head from List instance
}

Another valid alternative would be to change just one line:

public static INode head = new INode(8); // create instance during class initialization

I recommend to look at the difference between class (static) and instance variables, e.g. in the Java Tutorials (extract follows):

  • Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.

  • Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.

Upvotes: 1

someone
someone

Reputation: 6572

You have to initialize List object in main method

 public static void main (String[] args)
 {
   new List();
     INode a = new INode(8);
     int data = a.getValue();
    System.out.println(data);
    System.out.println(head.getValue());

}

Upvotes: 0

Related Questions