• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Static Method

 
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
newgen pras,

Is the result of ur program 20 20?
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic