aspose file tools
The moose likes Beginning Java and the fly likes static vs non-static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "static vs non-static methods" Watch "static vs non-static methods" New topic
Author

static vs non-static methods

Kathy Talley
Greenhorn

Joined: Mar 08, 2004
Posts: 3
I have read several entries in books about the difference between static and non-static methods, but I don't understand what is being talked about. There are a couple of entries on this site I read through and don't understand either. Can someone explain the difference very thoroughly and simply? I have read that you don't need an object to call a static method, and that you do need to create an object so that you can use it to access a non-static method. But I don't understand what this means... Help please!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10043
    
    6

See if this helps...
a static method is called by referring to the CLASS name, and then the method. One example is the Math class. you can NEVER have a Math object, the compiler won't let you say

but the Math class has lots of methods i want to use. since all the methods are static, i can call them like so:

Similary, your main() method MUST be static.
a non-static method must be called from a specific instance of a class. for example, the String class has a method with the signiatur

this tells you what character is at a specific position within a string. So, it's obvious i NEED a string to use this. it would make no sense to say

What string are we looking in? i need something like

notice this time, i called the method by using an actual object (more precisely an object reference), not the CLASS name.
does that help?
[ March 08, 2004: Message edited by: fred rosenberger ]

Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static vs non-static methods
 
Similar Threads
need some help with locking....
Q from valiveru
Why would we change a non static method to a static method.
static and non static
Calling non-static method from a static method