Im also surprised by the number of static methods in play, but why not if it works fine...
Actually i don't agree with your teacher.
If an object has no state (ie global variables) but just contains methods for exemple, it doesn't give you any benefits to use an object rather than static methods.
Except if you are planning to add a state later (state that should not be shared), or if you are using an interface and want to be able to switch easily the implementation, it's easier to use static methods...
JDK itself, apache commons or many frameworks are including static methods:
- StringUtils
- Pattern.matches(regex,input)
----------
Actually i guess you wonder what's about classes like JPA.java:
https://github.com/playframework/play/blob/master/framework/src/play/db/jpa/JPA.java
They use only static methods and keep a static state.
This could be strange, but actually for me it's a bit like using a singleton except the methods are used on a static context instead of an object.
The main difference is that you don't have to call getInstance() everytime.
I think this was designed like that for usability, because it is not user friendly to call "getInstance" and it's cool to be able to get easily a session everywhere (linked to the thread) instead of injecting the sessionFactory everywhere with xml or autowiring...
Your professor perhaps tells you to avoid using statics because it can be dangerous for your design if you don't use them right. But notice in many cases, replacing static methods by a singleton doesn't make your design better. Even if you now call the methods on an instance method, objects will still be tightly coupled...
So perhaps a rule should be to avoid using statics except when you don't really care about a tight coupling.
On this case, when you call JPA.xxx() methods, your code is tightly coupled to play framework's JPA class. But i don't think play is designed so that you would be able to easily switch from one framework to another without at least some rework...
It's a big difference with EJB3 spec or stuff like that: if the EJB3 entity manager's methods where static, you would be forced to tightly couple your code to the implementation by calling HibernateEntityManager.xxx() or ToplinkEntityManager.xxx().
In this case, there is a common interface (and we can't add static methods on interfaces).
----------
- That class is not part of a
specification used on other
frameworks.
- The JPA class has just
one implementation: the one done by
play. And they probably are not
planning to make a second one.
- Thus a
tight coupling to this Play class,
while you are using Play framework,
seems ok for me.
<exaggeration>
tag is not visible. :) – Chenee