Jump to content

java: using string value as variable name?

marten.aap2.0

Is it possible to use the value of a string variable as the name for a new variable? I need something like this:

private String var_name = "something";

public String [var_name] = "also something"; //[var_name] should be replaced by its value: something

public function get_something() {
  return something;	//this should return 'also something'
}

to make it a bit more clear, i need something like the PHP $$:

$var_name = "something";

$$var_name = "also something";

echo $something; //should echo 'also something'

 

 "Aeneas troianus est."

I'm allergic to social interaction in real life, don't talk to me in real life please.

don't forget to quote or tag (@marten.aap2.0) me when you reply!

Link to comment
Share on other sites

Link to post
Share on other sites

Not that I know of, what is the purpose for this? 

 

I edit my posts a lot.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, MrDrWho13 said:

Not that I know of, what is the purpose for this? 

 

I need to create multiple objects of the same class, and instead of having to type 'duck1 = new Duck; duck2 = new Duck; ...' I want to make a for-loop which can create them by just changing the number in the string variable and making new Duck.

 

(disclaimer, I'm not actually creating ducks)

 "Aeneas troianus est."

I'm allergic to social interaction in real life, don't talk to me in real life please.

don't forget to quote or tag (@marten.aap2.0) me when you reply!

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, marten.aap2.0 said:

I need to create multiple objects of the same class, and instead of having to type 'duck1 = new Duck; duck2 = new Duck; ...' I want to make a for-loop which can create them by just changing the number in the string variable and making new Duck.

 

(disclaimer, I'm not actually creating ducks)

Couldn't you create an array or ArrayList of ducks instead of individual variables?

I edit my posts a lot.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, MrDrWho13 said:

Couldn't you create an array or ArrayList of ducks instead of individual variables?

Do you mean something like:

private Duck [Duck1, Duck2, Duck3] = new Duck();

 

 "Aeneas troianus est."

I'm allergic to social interaction in real life, don't talk to me in real life please.

don't forget to quote or tag (@marten.aap2.0) me when you reply!

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, marten.aap2.0 said:

Do you mean something like:


private Duck [Duck1, Duck2, Duck3] = new Duck();

 

Not really. I'm going to use an arraylist here just in case you don't know how many elements will be created.

List<Duck> ducks = new ArrayList<Duck>();
for(int i; i>whatever; i++){
  ducks.add(new Duck());
}

And you access each duck with ducks.get(i);

I edit my posts a lot.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, MrDrWho13 said:

Not really. I'm going to use an arraylist here just in case you don't know how many elements will be created.


List<Duck> ducks = new ArrayList<Duck>();
for(int i; i>whatever; i++){
  ducks.add(new Duck());
}

 

ahh, ok, thank you!

I'll try to use it and learn a bit more about arrayLists

 "Aeneas troianus est."

I'm allergic to social interaction in real life, don't talk to me in real life please.

don't forget to quote or tag (@marten.aap2.0) me when you reply!

Link to comment
Share on other sites

Link to post
Share on other sites

Another way to do this is use an associative data structure, python and C# call this a dictionary. Java has the map class. All these are based on the concept of a key/value pair. The key being first part and what is used to index and retrieve the value.

Map<String, String> map = new HashMap<String, String>();
map.put("fname", "Mike");
map.put("lname", "smith");

map.get("fname"); //returns "Mike"

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×