File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to know if a certain variable is inside of a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to know if a certain variable is inside of a class?" Watch "How to know if a certain variable is inside of a class?" New topic
Author

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

Corey Hollaway
Greenhorn

Joined: Jul 21, 2004
Posts: 11


How would I know that the "kitKatBar" variable existed inside of the Corey class?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
Corey Hollaway
Greenhorn

Joined: Jul 21, 2004
Posts: 11
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!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
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.


I've heard it takes forever to grow a woman from the ground
Corey Hollaway
Greenhorn

Joined: Jul 21, 2004
Posts: 11
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to know if a certain variable is inside of a class?