Declaring static methods.
I have some questions about declaring a method as static. If I first write a paragraph about my understanding so far, followed by a code snippet hopefully somebody can help me out.
It is my understanding (limited) that only one class is loaded by the classloader. When creating an object, all its parameters are stored, but each object does not have its own copy of the methods its class defines, each method. If a method is declared as static then you do not have to create an instance of this class to call this method.
Code
/** Get table of codes.
*
* @return <code>Hashtable</code> populated with clinical code values.
*/
public Hashtable getCodeHashtable(HttpServletRequest request){
Hashtable htResults =new Hashtable();
try{
SpParameter[] spOut ={
new SpParameter(OracleTypes.CURSOR, 0)//po_cur_data
};
HOUtils utils =new HOUtils();
UtilDBquery db = utils.getUtilDBquery(request);
htResults = db.runGeneralStoredProcedureThrowsExceptions(null, spOut, SP_CodeList, null, true,true);
}catch (Exception e){
e.printStackTrace();
}
return htResults;
}
Questions
1) If each object does not carry around its own copy of the methods associated with its class, how does the jvm know which object is calling a method?
2) In the above code snippet, if I were to make the method static would this have implications if 2 people were to browse to the same page on the app server and run this method at the same time?
Thanks for any replies.
> 1) If each object does not carry around its own copy of the methods
> associated with its class, how does the jvm know which object is
> calling a method?
Each object stores one single pointer to its class description. The class
description contains all the methods (also the static ones). This is a bit
of a simplification but basically that's all there is to it. When you call
a method, the address of the method is looked up in that class description
and it's invoked. For non static methods there's a hidden parameter,
called 'this' so the method always 'knows' which object it (the method)
is invoked on. For static methods that hidden parameter simply isn't
present.
> 2) In the above code snippet, if I were to make the method static
> would this have implications if 2 people were to browse to the same
> page on the app server and run this method at the same time?
Who knows? This all depends whether or not the method (static or not)
is reentrant, i.e. does the method have side effects? Does the method
needs to be synchronized?
kind regards,
Jos
JosAHa at 2007-7-11 23:21:40 >

Thanks for the reply, the answer to question 1 was good, it has helped me a lot. I guess I really need to read more on syncronisation, as I dont think I understand its principles fully.
Could you give an example of when you would need to make a method syncronised? I understand that 2 thread running in the same memory space could access an object at the 'same' time, I guess more so with multi processing. Could they actually call a method at the same time? I just dont understand when you would need to make a method syncronised, and I dont understand at all why you would make a static method so, if I understand correctly that static methods should not affect any instance variables, and should not be tied to any object.
Thanks.
> Could you give an example of when you would need to
> make a method syncronised? I understand that 2 thread
> running in the same memory space could access an
> object at the 'same' time, I guess more so with multi
> processing. Could they actually call a method at the
> same time?
Yes. Even worse, to methods can try to update data at the same time.
> I just dont understand when you would need
> to make a method syncronised, and I dont understand
> at all why you would make a static method so, if I
> understand correctly that static methods should not
> affect any instance variables, and should not be tied
> to any object.
The static methods can still modify shared data (e.g. parameters etc)
kajbja at 2007-7-11 23:21:40 >
