The play() method is static. For a static method, Java doesn't need an instance to call the method, and if you provide a reference to one anyway, it doesn't actually use the reference. Even if the reference is null, as it is here. Instead, the compiler simply looks at the declared type of the reference, and uses that. So because reference variable "rope2" is declared as "Rope rope2 = null;", its declared type is Rope. And
is equivalent to
Regardless of the actual value of rope2.
posted 3 weeks ago
And as a matter of style, it's considered a bad idea to refer to a static method using an instance - precisely because it's confusing and misleading in cases like this. So you are encouraged not to write
Sam Peterson wrote:I've set the rope2 object to null...
No. You set the rope2 variable to null. There's no such thing as a "null object".
That's not just nit-picking. To understand Java properly it's necessary to understand the distinction between a variable and the object which it may refer to. "May refer to" indicates that a variable might not refer to an object, which is what happens when you assign null to the variable.
If I recall right from your earlier posts, you've already had problems because of confusion between variables and objects. Watching out for that distinction would be helpful.
posted 2 weeks ago
Paul Clapham wrote:If I recall right from your earlier posts, you've already had problems because of confusion between variables and objects. Watching out for that distinction would be helpful.
Alright. Lets shift gears into static then:
Line 14 did something I didn't expect. It printed out 8 instead of 2, even though rope1 and rope2 are different variable names. How can changes from one object update another static variable?
A static class variable means that there is only one variable PER CLASS, not per instance. So, no matter how many or how few instances you have, you'll only ever have one copy of a static member variable.
The proper way to access a static member is by using the class name and not an instance reference. So
would be the proper syntax, though the compiler lets you get away with using a reference, even a null one.
Remember there is no such thing as a null object. It means the reference points to null rather than to a “real” object. It doesn't tell you about how many objects there are, not even whether there are any objects at all.
Carey Brown wrote:. . . no matter how many or how few instances you have, you'll only ever have one copy of a static member variable.
“How few” includes no instances at all. That is why utility classes, which have only static members, usually have a private constructor; they don't need any instances. A class has one copy of each static member (field or method) and it can be accessed from any reference of the appropriate type. Which is why you can write code like this:-In line 2 you have a null which you are casting, and you are declaring a reference with type Arrays in line 3. You can call static methods on both; all you need is the type. You can't write this sort of thing:-...because Arrays only has a private constructor and that line 3 won't compile.
And I could probably write even worse code if you would like me to Remember this too: just because code appears to work, that doesn't necessarily make it good code.
Like everyone said in different words, you're not actually invoking the object when calling a static method, but the reference class of that object. That's why whether it is null or not is irrelevant and won't throw a NullPointerException.
posted 2 weeks ago
Please use the right terminology, NM: you invoke methods, not objects, so you should say you are not invoking the methods on an object.
You aren's using anything to do with that object (least of all if the reference points to null), but he declared type of the reference.
A (reference type) cast allows you to change the declared type of a reference.
I knew that guy would be trouble! Thanks tiny ad!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps