• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

how can I get the type of the object ?

 
Ranch Hand
Posts: 620
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello
if I have some variable and I like to know what is the stype of this variable
if there any "typeOf" method that gives me that info?
 
Ranch Hand
Posts: 536
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getClass()
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use getClass() to get the java.lang.Class object that represents the type of the object.

If you just want to know if an object is an instance of or extends a certain class, or implements a certain interface, you can use the instanceof keyword.
 
Ranch Hand
Posts: 1970
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above answers (suggesting getClass() and instanceof) are quite correct.

But, as this is the beginner forum, I thought I'd add a comment on use and misuse of these features.

Java is an object-oriented language (some would say not 100% so, but...). One therefore has powerful features like polymorphism available, to make objects of different types behave differently when sent the same message (method call). One should use those object-oriented features as much as possible.

It is usually bad practice to write code that conditionalises (if, switch etc.) on the type of an object. As much as possible, this should be replaced by something more object-oriented.

It's not a hard-and-fast rule. Although some Ranchers may disagree, I favour pragmatism. If a particular problem can be solved much quicker by using instanceof than by refactoring to use polymorphism, then it may be OK to use instanceof. But don't get too much into the habit.

If you were thinking of using getClass() or instanceof in an "if" or "switch" statement, you should probably go back and revisit your design.
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic