Hi, comrade
I believe that most of your questions do not have to do exactly with Java Web Programming, so I will reply them although my experience with Java Web Programming is limited.
Question 1: To avoid creation of DAO objects - I made all the methods static in the DAO Reply I do not believe there is anything wrong with this, actually there is
pattern known as Static Factory Method. Joshua Bloch, senior staff engineer from Sun Microsystems talks about it in his book: Effective Java Programing.
However, you might consider inheritance issues related to this approach since static methods do not inherit it could be issue if you pretend to extend your DAO objects in the future (I believe, in practice, when it comes to DAO objects, this rarely happen)
You can read an article about it in
JavaWorld Question 2:
As an OO principle - accepted this is not good but would this cause
any performance hits? Reply Your DAO Factory will not be loaded until it is actually needed. That would be the first time you call one of your static factory methods. Hence, you will save the time it takes to initialized a new Object since your DAO Factories just contain static methods and you will never create instances of your DAO objects.
If you need to provide some initilization code to your DAO (i.e. you need to provide a Connection DataSource), instead of using simply factory methods, you could transform your DAO into Singletons (GoF), by means of making the constructor private and providing a static getIntance() method that can initialize the the Object.
Still you might face inheritance issues directly related to Singleton as explained by GoF.
When it comes to executing the code in the method there is no difference between static and non-static methods.
Question 3:
Where are the static methods stored by application server ? Reply: Static methods are not stored anywhere. They belong to tha class, and they are available as soon as the class is loaded. The JVM will load the class the firt time that you use it (i.e. use a static member of tha class).
Classes are loaded by the JVM into memory using the application server classloader. Depending on the implementation of the server it is probable that every container used by the server has a proper classloader wich will be responsible of loading your classes into memory.
Question 4: If I create too many static methods - will this be an issue ? Reply: Basically, you are compromising inheritance of your of your Factories. Hence, it is an issue if you pretend to extend your classes. Beyond that you can have as many static methods as you want.
However, general programming principles might alert about badly implemented code. If your class has too many methods you may evaluate if all responsabilities executed by the methods really belong to the class. If you feel the class is carrying out resposabilities not related to it, then you can refactor some code into another class. It is the same principle if your mehtods are to long, then maybe you can refactor some code into another methods.
You may like to give a look to the Martin Fowler's book about Refactoring.
Question 5: Are static methods ever garbage collected ? Reply: Only objects are garbage collected. As your class is only composed by static methods there is nothing to collect.
However there is class object related to your class. This will be destroyed by the garbage collector only until the classloader that loaded it has been garbage collected. This is unlikely to happen during the existance of your Web Application unless the application server is stopped.
Question 6:
In case that the method in DAO takes a long time to execute ( connecting to database fetching data etc ) - would other invocations of the same method
experience a performance hit - i.e similar to a lock on the method and it not being accessible to other callers ? Reply: Unless you are synchronizing your methods, they must be always available to all callers. That�s to say, two different threads may be invoking the same method concurrently.
The administration of the resources your method uses is another story. For instance, if your are using only one database connection for all threads. In this case all requests to the database through this connection will be put in a queue, if the first
thread asks something from the database, the second thread request will be put in a queue until the first request is solved. In this case, you may consider using a connection pool.
But this is a different issue.
Regards,
Edwin Dalorzo
[ April 05, 2006: Message edited by: Edwin Dalorzo ]