This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static methods" Watch "static methods" New topic
Author

static methods

francis varkey
Ranch Hand

Joined: Sep 13, 2005
Posts: 166
Hi,
What is the consequence if all methods are static ?

thanks
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Do static methods belong to a class or an instance?
Can static methods access non static variables?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
shiva prasad.
Greenhorn

Joined: Jul 28, 2011
Posts: 9
Maneesh Godbole wrote:Do static methods belong to a class or an instance?
Can static methods access non static variables?


Static methods are class methods.
And static methods cannot access non static variables,
because by the time the non static variables are allocated memory the static methods get executed.
shiva prasad.
Greenhorn

Joined: Jul 28, 2011
Posts: 9
francis varkey wrote:Hi,
What is the consequence if all methods are static ?

thanks

If all the methods are declared as static then you are not required to create any object to that class ,
and you can directly access them using class name.
Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
shiva prasad. wrote:
francis varkey wrote:Hi,
What is the consequence if all methods are static ?

thanks

If all the methods are declared as static then you are not required to create any object to that class ,
and you can directly access them using class name.


... and that is one way you would help JVM manage memory from your side. If the abstraction is done only for services and not for data then such a class may not have any attributes in which case if you keep non-static methods in the class you would have to create an instance to call them, an empty object occupies atleast 4 bytes on heap because it has that "super" reference. Now why create an object if you don't need to store any data regarding that object? hence make all methods static so you can call them using class name.
Eg. Calculator class may not have any attributes if all we are concerned about are its methods; make all the methods static and you would call them as
Calculator.add()
Calculator.sub() ...

Library of Java has many such classes... good example is java.lang.System class where all the methods are static.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
The Math class might be easier to understand. You pass an argument to its methods, it calculates a result, returns it, and never keeps a record of the old calculation.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static methods
 
Similar Threads
Static methods can be overridden by static methods only?
static and non-static synchronized methods
method overriding
Inheritance of all members
What is the logic behind calls like JOptionPane.method()