Search code examples
methodscallprivateinvoke

How to call private method by other methods?


I am confused of calling a private method by another method(public) belonging to the same class.Once I have been told I gotta create an object of that class and then call the private method via this object but in one of my questions in this forum I have been told that I dont need to use object.

public class Train() {

    private void method1{......method definition..... }

    public void method2{......how to invoke method1??}

}

Can I simply call the first method inside the second method by using method1(); or should I invoke it by creating an object of the class and Object_of_Train.method1();.

Which one should I use?


Solution

  • Within the class you should be able to call method1();

    Outside the class you will need to call it from an instance of that class and will have access to public methods only