Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Static method

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does an object get created on the heap if i call a static method of a class.
i.e. lets say i write string str=ClassTest.methodReturnString(); does this mean that an object of type ClassTest will eb created on the java heap?
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Walker wrote:does an object get created on the heap if i call a static method of a class.
i.e. lets say i write string str=ClassTest.methodReturnString(); does this mean that an object of type ClassTest will eb created on the java heap?


No. That's why you don't need an instance of the class to execute a static method.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Vijitha is right.
for static method we don't required instance of class.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

"does an object get created on the heap if i call a static method of a class."

If you call static method on class of object, it is just reference of class it wont allocate any memory.
If you call it on object, certainly while creating object of call it will allocate memory for it.


 
Stanley Walker
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so i guess it means that if we have a contructor in that class that will not run, if we call a static method of that class. what about the static block in that class. while calling the static method it will most definitely run, rite?
 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Walker wrote:... what about the static block in that class. while calling the static method it will most definitely run, rite?


Yes. To run a static method the class should be loaded by the JVM. Static blocks run after the class is loaded to the JVM before any method runs.
 
Stanley Walker
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so why dont we mark all methods as static... wouldnt it be a better performance wise scenario.. no objects get created on the heap and the constructor code can be put in the static block..
i guess we would not be able to have overloaded constructors..
 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Walker wrote:so why dont we mark all methods as static... wouldnt it be a better performance wise scenario.. no objects get created on the heap and the constructor code can be put in the static block..


That's not the constructors are for. They are for creating objects. Use of static methods are very limited in OOP, they are mainly used in Factory methods, Singletons, defining constants (now Enums are prefered though) etc.. And creating objects are not much of a performance issue as garbage collector take care of unused objects with highly optimized compilers, JVMs we have these days.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic