How do you make a Node class field member if the Node class is in a separate file?

30 Views Asked by At

This is a total noob question since I cannot remember.

I want to make a homemade version of linked lists and my LinkedList class is as follows:

package stacks;

import java.io.*;

class Node<T>
{
    T data;
    Node<T> next;

    Node(T data)
    {
        this.data = data;
    }
}

public class LinkedList<AnyType>
{
    private Node<AnyType> head, tail;
}

I want to move the Node class to its own file in the same directory as the Linked List class. How to I reference the Node class as a field in the Linked List class if I do this?

Node class:

package stacks;

public class Node<T>
{
    T data;
    Node<T> next;

    Node(T data)
    {
        this.data = data;
    }
}

Linked List class:

package stacks;

import java.io.*;

public class LinkedList<AnyType>
{
    private Node<AnyType> head, tail;
}

This doesn't work if the node class is in a separate file. Since the "Node" is a field member of the LinkedList class, I do not want to instantiate it yet. I just want a reference to the class.

This is a very basic question, I know. I haven't done object oriented programming in a while so I don't really remember these basic things.

This is the error I'm getting:

LinkedList.java:13: error: cannot find symbol
    private Node<AnyType> head, tail;
            ^
  symbol:   class Node
  location: class LinkedList<AnyType>
  where AnyType is a type-variable:
    AnyType extends Object declared in class LinkedList
1 error
0

There are 0 best solutions below