Reputation: 2195
today i have stumbled on a code which i have seen in my project and was worried looking into it. I dont realize why they have made these as static methods as they change state of object within them.
below is the code
@Controller
CruiseController{
getCruiseSearchResults(){
//prepare cruise serach request, static method in CruiseHelper
CruiseSearchRequest cruiseReq = CruiseHelper.prepareRequest();
...futher impl
}
/** my helper class which has utlity methods */
CruiseHelper{
public static CruiseSearchRequest prepareRequest(){
CruiseSearchRequest cruiseRequest = new CruiseSearchRequest();
// all below methods are static
setCruiseTypeandDestination(cruiseRequest)
setStartAndEndDate(cruiseRequest)
setShipAndDeparturePort(cruiseRequest)
setDurationAndAccesiblity(cruiseRequest)
setPromoType(cruiseRequest)
setResultPreferences(cruiseRequest)
return cruiseSearchCriteriaDTO
}
static void setCruiseTypeandDestination(CruiseSearchRequest cruiseRequest){
/** changing the state of object in static method */
cruiseRequest.setCruiseType("ABC");
cruiseRequest.setCruiseType("Alaska");
}
//.... further static methods as above, all of them
//change the state of cruiseRequest
}
So i know that, above methods should not be static as they all have properties of each request. But the code is working and has not failed on any load test performed.
my important question is : "can this above code be considered ??" and "can this fail, if yes. then in what scenario ?"
Upvotes: 1
Views: 5778
Reputation: 2504
Indeed, these methods change the state of an object, but it's an object that is given as a parameter to them, which is perfectly valid and makes sense.
static
means that the method is bound to the object definition (the class) and not to any specific object instance. Thus static method cannot change the state of it's own object as it simply does not have an instance to work on (it does not have a this
).
I suggest you read this about static and class variable : Understanding Class Members
Upvotes: 2
Reputation: 50041
I dont realize why they have made these as static methods as they change state of object within them.
Because they're methods of a class called CruiseController
. They are modifying instances of a CruiseSearchRequest
, a different class. You could not do cruiseSearchRequest.setCruiseTypeandDestination();
, because the method setCruiseTypeandDestination
isn't on the CruiseSearchRequest
class. So, instead, it receives the CruiseSearchRequest
object as a parameter, and since it isn't tied to any instance of CruiseController
, it is a static method of that class.
You could make the methods non-static if you moved them to the CruiseSearchRequest
class. However, you don't need to. There is absolutely nothing technically wrong with modifying objects in a static method. It might or might not be a good design for your particular program, but it will not "fail".
Upvotes: 1
Reputation: 4179
Methods in Spring shouldn't be static not because they won't work but becouse it is a bad decision in terms of your application architecture. Static methods are wrong decision in terms of unit testing - it is harder to mock static methods than objects. Also I think intensive usage of static methods breaks the concept of dependency injection and the code becomes more tightly-coupled. Here is a nice article on this topic.
Upvotes: 0
Reputation: 36304
Static methods are used to imply that that method doesn't need an instance of the class to be called. For example consider the String class. It can still change the state of any object.
replaceAll()
is not a static method as it needs an instance to work on. where as valueOf()
isn't as it doesn't need a String instance.
I suggest you revisit the basics of Java.
Upvotes: 1