Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods

 
francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is the consequence if all methods are static ?

thanks
 
Maneesh Godbole
Saloon Keeper
Posts: 11088
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do static methods belong to a class or an instance?
Can static methods access non static variables?
 
shiva prasad.
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ashutosh Limaye
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic