• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a way to find the type of the object reference used?  RSS feed

 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there, i am trying to find a method that provides the type of the object reference used?

For example the classic :



How can i find the type of "ref". I am looking for a method that returns "Parent" and not the type of the referenced object(Child).

I have checked with getClass() , toString()( to see if the hashcode lists it), looked at the Object and Class classes description + tried to figure out if "ref" as a ... lets say variable is inheriting something that can provide me some functionality. Nothing came up.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only do this for fields and method parameters, using reflection, not for local variables.

Why not? Because it's useless. The formal type of a local variable is only interesting at compile-time. You get its type by "looking at it with your eyes".
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply.
I was trying to print out a report of different combinations of different levels of inheritance , types of references and types of access. Just to showcase hiding/overriding and so on. It is not 2 or 3 variables used, so "looking at it " pretty much is against the whole purpose.
Basically a line printing out :

"Currently working with object of type Child, referenced by a reference type Parent" was what i needed this for.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the following to get the actual type:


This prints child. As Stephen points out, you'd have to hard code the reference type of Parent.
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, the object type is clear. The hardcoding of the reference part was something i was hoping to avoid, but i guess that it is my only option.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're reasoning about code and not about a program, one way to solve this is to write a program that parses the source file.

Not an easy task though.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is part of the philosophy of an object language. You declare references with a type, but the actual objects have a runtime type of a subtype of that.

Subtype includes the type, so Parent counts as a subtype of itself, for the purposes of this question. Just as in set theory, S ⊆ S is always true. Every set is a subset of itself.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean to do something like the following?

The output for this would be
Currently working with a Child object, using a Parent reference

You'd still have to hardcode Child.class and Parent.class but at least this is amenable to refactoring (rename) with an IDE vs. if you bake the names into the strings you're displaying.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could even make that more sensible by doing this:

Again, still some hard-coding involved but at least it can be refactored automatically if you decide to rename the class for some reason. Basically, you're using compile-time info for the declared type and runtime info for the object itself.
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The part i was struggling with was becuase i was trying to do something like that




But the answers here were clear on what are my options . Thanks a lot to everyone.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From there, I would explore the possibility of using a custom annotation to make this even more sensible. Seems like a good exercise, don't you think?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get just the Object name without parsing:
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i just realized that from Junilu's post. I tried to add an edit to it to thank him , but it did not allow me . Are there limitations to editing your own posts?
Junilu , as the exercise is the whole point , i will definitely look into that.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there limitations to editing your own posts?

I believe you can't edit your post once it's been replied to.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a problem.

Re editing restrictions, once someone has replied to your post, only moderators can edit it. This prevents what we call "ninja edits" that can render the rest of the thread nonsensical. Moderators who edit a thread and make it nonsensical anyway are taken behind the woodshed for an intervention or a heart-to-heart talk with a Sheriff or the Trail Boss.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!