Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
Zachary Griggs wrote:Also, the method can be static
Zachary Griggs wrote:my opinion is that is a method does not reference this, it should be static since there's no need for each instance to have its own copy (as each copy will behave identically).
Is there any harm to using static like this? ... Imo it's a question of having the appropriate "scope". Static/instance isn't exactly a scope, but that's the best way I can describe it. Instance methods exist to use the this reference, and if it's not used, there is simply no need to let it have this in the first place. It's a reference that the method doesn't need, so the method shouldn't have it. But this only applies when the instance and static versions behave exactly the same (as is with the method in OP).
Static methods are procedural! In OO language stick to OO. And as far as Math.abs(-5) goes, I think Java got it wrong. I really want to write -5.abs(). Ruby got that one right.
I'd guess that the reason some beginners are taught to use only statics is that using instances requires instantiating your own class, which is not necessarily an easy concept when you're just beginning.
I wrote:I think that if you're going to teach an object-oriented language, the first thing you want to teach is why do you want to think in terms of objects. This is the context that kids lack and want for the most. I think this context is what older generations of programmers who grew up learning and writing procedural code first have that the many in the newer generations of programmers lack.
Older generations of programmers understand the problems that come with procedural code and therefore can put object-orientation into context. Yes, it was a paradigm shift but once the shift was made and the mindset around objects was fully grasped, the benefits were obvious. The newer generations of programmers never had to undergo that paradigm shift. All they know or are taught is that's the weird way it's supposed to be done.
I'm not entirely agree with that.
Junilu Lacar wrote:I will think nothing of writing and refactoring to methods like this:
By your reasoning, this should be a static method. I would never think twice about leaving this as an instance method though.
@OP, there are many arguments against static methods so be sure you are at least aware of the consequences of having too many static methods in your program, especially if you want to write object-oriented code.
Zachary Griggs wrote:I don't see why Java being OOP means you can only use object oriented programming. Sometimes a procedural method is what fits best for what you want to do, if the code inside works as a simple procedure. Ignoring static altogether is just ignoring one of the options Java gave to us.
Even your isValid example - that does not have any context to a specific instance of your class, but to the class itself. Making it a static method in the class says exactly that, it forces it to belong to the class in general rather than each instance.