I am very new to programming with Java and I had a question with the usage of objects of a class. I have done a program on DLL as shown below :
class DoublyLinkedList
{
Node1 head;
void createFirst(int d)
{
Node1 newnode = new Node1(d);
head = newnode;
}
void appendNode(int d)
{
Node1 temp = head;
while(temp.next!=null)
{
temp = temp.next;
}
Node1 newnode = new Node1(d);
temp.next = newnode;
newnode.prev = temp;
}
void display()
{
Node1 temp = head;
if(head==null)
{
System.out.print("Empty List");
return;
}
System.out.print("Nodes are ...");
while(temp!=null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
}
void main()
{
DoublyLinkedList obj = new DoublyLinkedList();
obj.createFirst(2);
obj.appendNode(5);
obj.appendNode(7);
obj.display();
}
}
My question is inside the main() instead of making an object as I was taught, what will happen if I just call the functions directly as shown below :
void main()
{
createFirst(2);
appendNode(5);
appendNode(7);
display();
}
I want to know the problem, the relation that this object creates etc. Someone please help.