Posts: 266
Threads: 239
Joined: Apr 2011
we can not call the non static methods from a sttic methods
is it true statement???
If so than how we are calling the methods from our main programme
because main is static method
kindly clerify this point fully
thanks
Posts: 9,583
Threads: 8,089
Joined: Apr 2011
Dear Student,
All static methods work alike and you are not right in sense that we call non static method in static method by providing it a reference to the belonging object.
e.g. look at following code:
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main m1 = new Main();
m1.disp1();
NewClass.disp();
}
public void disp1(){
System.out.println("its a non static method");
}
}
public class NewClass {
public static void disp(){
System.out.println("its a static method");
NewClass n1 = new NewClass();
n1.disp1();
}
public void disp1(){
System.out.println("its a non static method");
}
}
Output:
run:
its a non static method
its a static method
its a non static method