Reputation: 1
I have to create a LinkedList of objects from scratch and I simply can't get my head around it, I'm not sure if i need a class for the linkedlist and the node or if i can do it in the object class itself. I'm also doing this on JavaFx using scene builder so i need to add objects through user input from the controller class.
I've tried separating the node and linkedlist into there own classes but that just confused me further.
This is my current Show object fields and constructor
public class Show {
private String title;
private int runningTime;
private String startDate, endDate;
private int ticketPrice;
public Node head, next;
public Show (String title, int runningTime, String startDate, String
endDate, int ticketPrice) {
this.title = title;
this.runningTime = runningTime;
this.startDate = startDate;
this.endDate = endDate;
this.ticketPrice = ticketPrice;
next=null;
}
When i try to call the head which i've made public in my Show class it gives an error "Non-static field 'head' cannot be referenced from a static context"
I'm looking for the correct structure for object specific linkedlists.
Upvotes: 0
Views: 909
Reputation: 537
Adding the following method to your 'Show' class will work. You couldn't reference 'head' directly here because main is not executed in the context of a Show object and hence cannot access any class member variables. However, it can access these member variables qualified by an instance of the Show class. Please note, that I'm not advocating that you expose your implementation details like this, but that is another discussion.
public static void main(String args) {
Show show = new Show(....);
show.head = new Node(...);
}
Upvotes: 0