Yes, but why do you want such static methods? You would call methods like takeMoney(...), giveChange(...), getCurrentTotal(), etc.Monica Shiralkar wrote:. . . call methods like CashRegisterHelper.staticMethod1, CashRegisterHelper.staticMethod2 etc ?
Campbell Ritchie wrote:You want to see your objects working; you should already know your helper class works correctly.
Campbell Ritchie wrote:No. I meant that you shouldd see the new instance methods working as a priority.
Let's look at a different example:-Now explain the remainder of that class, including my not overriding equals(), hashCode(), and toString().
Monica Shiralkar wrote:Yes, but to me those methods which I mentioned had looked suitable to be static methods and not instance methods.
It makes sense to have a method as static , it only does what it does and does not set any instance variable.
If the static method requires a method parameter to do what it does, you should consider adding an instance method to the class of that method parameter instead.
Monica Shiralkar wrote:... shouldn't I simply make that static and that piece of work done?
Monica Shiralkar wrote:But if all we are doing is reading something from the database, whey do we want to tie that to an instance instead of just having it static?
Paul Clapham wrote:
You have followed Stephan's advice here;
Monica Shiralkar wrote:But if all we are doing is reading something from the database, whey do we want to tie that to an instance instead of just having it static?
Suppose I have a method [...] which "requires a method parameter", then shouldn't I simply make that static and that piece of work done?
Monica Shiralkar wrote:But if all we are doing is reading something from the database
Stephan van Hulst wrote:
No. Your example doesn't affect application state.
Monica Shiralkar wrote:That's why I wrote that I wanted it to be Static.
Agree.Paul Clapham wrote:. . . It's unfortunate that people are often taught . . . only . . . static methods. For a lot of people it seems like a big step to switch over to programs which use objects, apparently.
I would add to that, or when they are being used as functions and the class only acts as a container for the method. The methods in java.lang.Math are like that; since the introduction of static import you can write sin(x) and get its sine. A more object‑oriented design might have been to give Math an argument field:-Maybe this design of the Math class follows C where such functions can exist all on their own in a math.h file.Paul Clapham wrote:. . . . Static . . . methods should only be used when they specifically apply to the workings of the entire class . . . .
Monica Shiralkar wrote:That's why I wrote that I wanted it to be Static. You also said that if it doesn't affect application state , it can be static . So I am trying to understand why did you say "No" in that case.
It can't come too soon.Stephan van Hulst wrote:. . . treat primitives as objects . . . when project Valhalla is finally done.
Campbell Ritchie wrote:
I would add to that, or when they are being used as functions and the class only acts as a container for the method.Paul Clapham wrote:. . . . Static . . . methods should only be used when they specifically apply to the workings of the entire class . . . .
Not sure about what you mean here. Are you referring to method definition ?anmol singh wrote:1. If you are writing utility classes and they are not supposed to be changed and ...
Disagree, this is not always true and might need a non-static methodanmol singh wrote:4. If there is some code that can easily be shared by all the instance methods, extract that code into a static method.
I didn't read in much, but that URL covers topics related to static class and not static methods. Quite honestly, I find the indentations of the code posted on that tutorial quite confusing.anmol singh wrote:For more details you can read about ...
Static variables and methods should only be used when they specifically apply to the workings of the entire class and not to the working of an individual object.
You're fighting against the Object Oriented nature of Java.
Monica Shiralkar wrote:But the below code is not related to object. All it is doing is taking a name and creating a greetings message using it. Thus, can I use static for the below?
Monica Shiralkar wrote:But the below code is not related to object. All it is doing is taking a name and creating a greetings message using it. Thus, can I use static for the below?
Paul Clapham wrote:
Since it doesn't compile there's no point in asking the question.
Stephan van Hulst wrote:the need for static utility methods that are reused by multiple classes should not arise very often.
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
|