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

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: 79969
396
  • 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.
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic