I can create an object in the main method and call the other method with the object I created.
public class Obj{
public static void main(String[] args) {
Obj obj = new Obj();
obj.yourNameIs();
}
void yourNameIs(){
System.out.println("TY");
}
}
However, i have to change yourNameIs
method to static if I want to call it without creating an object.
public class Obj{
public static void main(String[] args) {
yourNameIs();
}
static void yourNameIs(){
System.out.println("TY");
}
}
I understand you cannot call a non-static method in a static method in this case the main
method. But why I can create an object in the main method and then I can call a non-static method such as yourNameIs()
? I mean why I do not need to change yourNameIs()
to static method?