wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Calling variable or methods with Strings? 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 » Beginning Java
Bookmark "Calling variable or methods with Strings?" Watch "Calling variable or methods with Strings?" New topic
Author

Calling variable or methods with Strings?

lil Yaure
Greenhorn

Joined: Feb 25, 2010
Posts: 23
Halo everyone

I'm new here and I'm wondering how to call variables or methods with Strings in Java?

Below is what I normally do in ActionScript3(flash) using this["String"];

var cButtons:Vector.<Button> = this["_" + name + "Buttons"];

so I can easily iterate or change the name variable to access any other variable.
And using this["myMethod" + num](); to call a bunch of methods

But it doesn't seem to work in java
How do you do it in java?

Thanks


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

That probably isn't something you want to be doing in Java. Usually there are better ways than the literal copy of the ActionScript code. Java tends more to have classes which do things than tricky syntax which does things.

For example if you have a mapping from Strings to Buttons, then you would simply create a Map<String, Button> object and use its built-in methods.

And I can't imagine any language where you would have methods named myMethod1, myMethod2, and myMethod3 and want to call them sequentially. That's probably more ActionScript providing syntax options which lead you down the bad-design path.

I would suggest going through the Java tutorials for beginners. That way you will see what Java programs look like, and you won't end up speaking Java with a heavy ActionScript accent.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40


Never worked in actionscript, but based on what you described, actionscript objects seems very similar to javascript objects -- in that it can be used like an associative array. It may be better to not do that in java, and instead, actually have the Java object have an associative array. In java, these types of arrays are implemented as Maps.... such as the java.util.HashMap class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
lil Yaure
Greenhorn

Joined: Feb 25, 2010
Posts: 23
I'm using the newest version of actionscript and its oop as well, with packages, inheritances etc.
I know how to use hashmaps, lists, and stuff already, actionscript have dictionary class similar to this too, but it seems like an extra step to take

I'm not really calling methods1, methods2, methods 3 etc, What I'm wanting to do is to make the code expandable and otherwise I will just have a massive big list of switch case...and if else

say I have a variable called name, another called nameTextField, another called nameBar, another nameWhatever
then I can just iterate through an array ["name", "password", "address", .....]

for(int i=0; i<array.length; i++){
this[array[i] + "TextField"];
this[array[i] + "Bar"];
this[array[i] + "Whatever"];
}

each of them are the actual reference to the variable, so I can do something like this[array[i] + "TextField"].subString(); and I can easily add stuff to the array to expand it


what I'm doing is a lot more complicated than this actually, its a massuser program, involves gettings lots of data from the database, and needs to be expandable
involves both actionscript and java

want a quick and lazy way to do this for the java part , wouldn't using HashMaps make it more complicated, I normally only use HashMaps for objects
is there not a way to get a variable e.g. int _type when I just have a string with the name "type" ?

thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Adam Yaure wrote:
want a quick and lazy way to do this for the java part , wouldn't using HashMaps make it more complicated, I normally only use HashMaps for objects
is there not a way to get a variable e.g. int _type when I just have a string with the name "type" ?


If you don't want to use a hashmap, and want to use the variable names directly, then you can use the reflection library.... but be warned. A couple of hours with the reflection library, and hashmaps will definitely not be "more complicated".

Henry
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

Adam Yaure wrote:I'm not really calling methods1, methods2, methods 3 etc, What I'm wanting to do is to make the code expandable and otherwise I will just have a massive big list of switch case...and if else


Well, again, you can replace a massive switch-case or if-else in Java by a Map whose keys are whatever it is in your design and whose values are Runnable objects.

But the bottom line is that Java is not a scripting language so it's not a good idea to try to pretend that it is.
lil Yaure
Greenhorn

Joined: Feb 25, 2010
Posts: 23
lol reflection is insane

guess I'll need to put them into maps
thanks


btw this forum is sooo active @@
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Flash actionscripts use javascript.

Scripting languages like javascript are executed 'on the fly' meaning you can have an 'eval' function that can treat a string thats dynamically
generated on the fly as a valid command statement.

Thus, on the go, you can create functions, arrays, etc. directly through strings and even allow method calls to be done in this manner.

While java, is a compiled language, you cannot do functionality like adding new properties/functions to existing objects, you can only utilise
the methods that are already declared in code.

Though this may seem like a drawback to you, it actually amounts to cleaner coding.
I do not want to arise the age old debate between scripters and language coders ...

personally I dislike the use of prototypes, but thats just me


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
lil Yaure
Greenhorn

Joined: Feb 25, 2010
Posts: 23
salvin francis wrote:Flash actionscripts use javascript.

Scripting languages like javascript are executed 'on the fly' meaning you can have an 'eval' function that can treat a string thats dynamically
generated on the fly as a valid command statement.

Thus, on the go, you can create functions, arrays, etc. directly through strings and even allow method calls to be done in this manner.

While java, is a compiled language, you cannot do functionality like adding new properties/functions to existing objects, you can only utilise
the methods that are already declared in code.

Though this may seem like a drawback to you, it actually amounts to cleaner coding.
I do not want to arise the age old debate between scripters and language coders ...

personally I dislike the use of prototypes, but thats just me


eval() is gone now,,I use strings to call variables and methods, I can't create them with strings on the fly...

=3= you should check out the newest version of ActionScript 3, you'll be surprised how much it has changed since the last version (I hate prototypes too and old actionscript)
http://www.infoq.com/articles/actionscript-java

it is real oop and compiled (apart from constructor and $init is interpreted), it has classes, inheritance, interfaces, namespace, events ...etc tho lacks concurrency...
not prototypes!

I still do declare everything private, after I've declared a global variable _something doesn't it makes sense I should be able to call it with "_some" + "thing", I like dynamic class tho =]
I learnt java first actually, really hated swing then, so went on to actionscript3, which can do things much faster(for me)

Anyway, they are for different things anyway, one for server and one for client side.
Guess I can live with indexing, got to learn more about concurrency now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling variable or methods with Strings?