views:

224

answers:

4
+2  Q: 

Reflection in Java

How can i access a varible defined inside a method by using reflection?

I have to create criteria in hibenate. Database is attribute based database. Let's talk about a movie. A movie can have many language and many genre. e.g.

Movieid property value

1 Language Hindi

1 Language English

1 genre action

2 genre comedy

let's there is thousand record of differnt movie. user select a set of genre(action) and language(hindi, english) to filter the result. Now, i have to create criteria in this order : expression = (property= english and genre = action) or (property= hindi and genre = action)

For the implementation, i have to lookup into a local varible defined inside a for loop condition.

+1  A: 

You can't.

Hank Gay
Damn. My exact answer. :)
Bombe
I beat Yuval A to the punch by about 7 seconds by my watch, but he had to go and add useful information and such ;-)
Hank Gay
And we're all about useful information, aren't we? :D
Yuval A
+18  A: 

You can't. If it's defined in a method, is it a local variable defined in that scope only. Since the method had no state outside of its scope, you have nothing to access.

Accessing a class member (which, by definition, is a state) you can access via "regular" reflection.

Yuval A
A: 

But, Pray tell me why do you want to even attempt that ?

Rig Veda
A: 

I'm guessing that your data for these movies are stored in an RDBMS. Just do a simple relational query to get exactly the results you need. This problem is a perfect example of the sort of for which people developed relational database management systems.

Curt Sampson