This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static method" Watch "Static method" New topic
Author

Static method

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
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?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
yes Vijitha is right.
for static method we don't required instance of class.
Pallu Sagar
Greenhorn

Joined: Dec 04, 2009
Posts: 16
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.



-Sagar
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
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

Joined: Mar 24, 2008
Posts: 3817

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

Joined: Sep 23, 2009
Posts: 87
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

Joined: Mar 24, 2008
Posts: 3817

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.
 
wood burning stoves
 
subject: Static method
 
Similar Threads
on STACK or on HEAP?
where do static variables live ?
variables on the stack or heap
[eckel] on string initialization
question about static variable's residence