• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Method

 
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it true that static methods can call only static methods? If yes, then how do we call non-static methods from main() method?
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it's true, nut not only static methods, we can also call static fields.
In order to call a non-static method from a static method we must create an object of the non-static method's enclosing class.

Cheers, Marzo.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anikanchan: If yes, then how do we call non-static methods from main() method?

Like this:


Joyce
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If static method can be called from another static method ..how does this code works .. pls explain me ..

 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because a static method is being called from a non static method and not the other way round.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Absolutely true Anikanchan!
You can never have non-static code inside a static block.

Looking at the example....


public class Cow
{
public static void main(String[] args) {
Cow c = new Cow();
c.moo();
}

// an instance method
public void moo() {
System.out.println("where is my dinner?");
}
}



I guess it is running fine because the object c is created within the main() itself i.e. whatever be the case "c" will have its scope only and only within main(). So there is no point in saying "c" as a static or non-static thing.

I dont know but i think if we have "Cow C" declare outside somewhere and used inside main (why just main, any static method) then there must be an alarm!!!

Well, I havent tried it any comments guys.......
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
newgen pras,

Is the result of ur program 20 20?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic