1

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • That main is not a valid main (at least, not if it is intended to be an entry point of your program, as that should be `public static void main(String[] args)`, not just `void main()`). If you had a correct main, the problem would become immediately obvious (because it wouldn't compile). In your last code, you are calling the methods on `this` implicitly, which is the instance of `DoublyLinkedList` you called `main()` on, while in your original code you call it on the instance referenced by `obj`. – Mark Rotteveel Dec 27 '21 at 08:25
  • 3
    _what will happen if I just call the functions directly_ There's one way to find out, isn't there? – Abra Dec 27 '21 at 08:29
  • 1
    Does this answer your question? [Calling Non-Static Method In Static Method In Java](https://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java) – Oussama ZAGHDOUD Dec 27 '21 at 09:13
  • @user16320675 I have actually tried that before posting my question. The fact is that if i create an object, but calling some function without it, then that function is not working. But it works when all functions are called without object and the object is not created. So, you see I am getting output in both the ways but I want to know the difference and why the patial object and function combo not working. Please help. – Surya Majumder Dec 27 '21 at 09:26
  • @SuryaMajumder You should read the basics from a book , try to search about what is the difference between static and instance methods , search for what is the meaning of the word static in java, what is an instance ... thanks – Oussama ZAGHDOUD Dec 27 '21 at 09:47
  • @OussamaZAGHDOUD thank you so much for the topic to search on ; difference between static and instance methods ; finally got my answer thank you. Thanks a lot. – Surya Majumder Dec 27 '21 at 09:52
  • Welcome , static method is like a shared method, you can call it just using the reference to the method , or using a reference of an instance , or using a reference of the class , , instance methods are methods of objects or instances , you can call them just using a reference of the instance – Oussama ZAGHDOUD Dec 27 '21 at 10:56
  • Wow! Thank you a lot @OussamaZAGHDOUD for the beautiful description. Thanks a lot. – Surya Majumder Dec 28 '21 at 10:15

0 Answers0