• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How to call static methods from non static methods

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

all static method from non static method?


Just call it the usual way : MyClass.myStaticMethod();
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 3461
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can even call it in the traditional way, ClassName.methodName(), which does not cause any harm
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there any problem to do that :
new Class.NonStaticMethod()?
i mean from the preformance point of view?
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic