• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to call static methods from non static methods  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all

i have class that holds static methods , this is utility class and i need one of the actions

but i need it from non static method . is there any way to over come this ?

call static method from non static method?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all static method from non static method?

Just call it the usual way : MyClass.myStaticMethod();
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Non-static method of which class?

Even if its in the same class and though its not advisable, you are allowed to call the static method from a non-static method wherein the reverse case is not true.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't matter what type of method that is calling the static method is. The calling mechanism is the same. I can't think of a case where the method that gets called should ever care or need to know where the call is coming from.

If you are calling the static method from outsize the class that the static method belongs to, it is NAMEOFCLASS.NAMEOFSTATICMETHOD();

If the call comes from an object from the same class as the static method: NAMEOFSTATICMETHOD() works just fine. However, you can use the class name in the call if you want, like in the second paragraph.
 
arulk pillai
Author
Ranch Hand
Posts: 3417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have understood your question correctly

You can call a static method from a nonstatic method as follows:




But to call a non-static method from a static method you need to create an instance of that class:


 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can even call it in the traditional way, ClassName.methodName(), which does not cause any harm
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any problem to do that :
new Class.NonStaticMethod()?
i mean from the preformance point of view?
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean " new MyClass().NonStaticMethod() "?
This creates a new instance of MyClass, so if your constructor does some time consuming stuff, it will unnecessary decrease performance.
At any rate, this is not a good coding style.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!