Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position


Join the forum, it's quick and easy

Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position
Virtual Position Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

We can not call the non static methods from a sttic methods

View previous topic View next topic Go down

GMT + 3 Hours We can not call the non static methods from a sttic methods

Post by WikiLeaks Sun May 29, 2011 2:40 pm

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
WikiLeaks
WikiLeaks
Monstars
Monstars

Posts : 26
Join date : 2011-05-29

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum