• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

A null object can produce output?

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've set the rope2 object to null:

And the output is:
swing
swing

How can the rope2 object instance, being set to null, still call the method?
 
Rancher
Posts: 3418
34
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Rancher
Posts: 3418
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

but instead

 
Marshal
Posts: 24943
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Sam Peterson
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Saloon Keeper
Posts: 6758
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Saloon Keeper
Posts: 6758
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 299
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!