• 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 methods

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What is the consequence if all methods are static ?

thanks
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do static methods belong to a class or an instance?
Can static methods access non static variables?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
A tiny monkey bit me and I got tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic