Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to know if a certain variable is inside of a class?  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How would I know that the "kitKatBar" variable existed inside of the Corey class?
 
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) read the sourcecode
b) read the documentation

you could use
c) use reflection
d) guessing

But a class without sourcecode and without docs is useless.
Throw it away.
 
Corey Hollaway
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I use reflection to see if kitKatBar existed inside of the Corey class?

This is the reason why I want to know:
------------------------------

I'm going to have a lot of people in a program using polymorphism. BUT, only some of them are going to be Athletes! And in main I'm going to have this:

People[] myPeople = new People[2];
myPeople = new Corey();
myPeople = new Sally();
for(int i=0; i<myPeople.length; i++){
if(They have the kitKatBar variable)
System.out.println(myPeople[i].name + " is an Athlete!");
}

Docs & source code can't help with this one! I need reflection? Thank you for your time! And your reply Stefan Wagner Peace out!
 
Sheriff
Posts: 67261
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a completely horrible way to do it. How about a boolean property that declares whether they are athletes or not... or use the polymorphism you mentioned to create a subclass of People that is Athlete?
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or, to use it as is, you could check instanceof Athlete. If it is, it has the variable. Although, i second Bear's assesment of the idea as a whole.
 
Corey Hollaway
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bear Bibeault and Joseph George. I third Bear's approach. But let's say I was crazy, and I wanted to make my coworkers hate me (but I that's because I'm using this technique in a bad way, but next time it will be really good!). I like what you said about instanceof!

And I got it! I was working with the instanceof VARIABLE, but all I needed to do was if People[i] instanceof Athlete! I thought I couldn't do that! Thank you! Here's all of the code for who needs it!

Athletes.java:
-----------------------------------------

[ July 21, 2004: Message edited by: Corey Hollaway ]
 
It's just a flesh wound! Or a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!