0

For the sake of simplicity, I have written a very simple method of what I am attempting to do. I want to send this method a string, then make a new variable whose name will BE the string. I know this is completely invalid with the current way I am doing it, but HOW can I do this? (I understand that this method looks completely stupid and looks like I have no idea what I'm doing, but I do. My program I am writing really needs this capability. PLEASE help.)

public int whatever(String s)
{
        int s = "hello";
        return s;
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 4
    Seems like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. Can you explain why do you need this behavior? – fracz Sep 19 '14 at 21:26
  • 1
    I am not entirely sure what you want to achieved but it looks like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Consider describing *what* you actually want to achieve, not only *how* you want to do it. For now what you describe seems impossible, but one of way around could be using `Map`. – Pshemo Sep 19 '14 at 21:27
  • 1
    **"My program I am writing really needs this capability."** Why? Does your program have some way to know the name of a variable? What could you do if you know that method `whatever` had a local variable named `hello`? How would you do it? – Joshua Taylor Sep 19 '14 at 21:27
  • I agree with the other commenters that this suffers from the XY problem. The way your example method works, the caller of the method could never notice what the (local) variable was called. – OhleC Sep 19 '14 at 21:28
  • 1
    @ohlec That said, with reflection we *can* determine whether there's a *field* with a given name. I wonder if OP is interested in doing something like that. (Not that the code in the question makes that clear.) – Joshua Taylor Sep 19 '14 at 21:30
  • Your code as written does not make sense. You cannot assign a String value to an int. You will have to show what you're trying to achieve from both sides (the method and the user of the method). – Jim Garrison Sep 19 '14 at 21:31
  • If something like this would be needed frequently there would be an easy way to do it. As there is none, there must be another way. – Gumbo Sep 19 '14 at 21:31
  • **"I understand that this method looks completely stupid and looks like I have no idea what I'm doing, but I do."** Part of writing a good Stack Overflow question (and debugging in general) is being able to describe a problem thoroughly and clearly. If you think that the question as you've written it doesn't do that, it would probably help everyone involved if you can show a more realistic example, and explain what this desired functionality would allow you to do. – Joshua Taylor Sep 19 '14 at 21:32
  • @JoshuaTaylor True, with reflection it's possible to identify a field that's named like the passed String argument. I have a very hard time imagining a valid reason to do so, so it would be nice to learn more about the actual problem. – OhleC Sep 19 '14 at 21:32

1 Answers1

0

Essentially you want to create or modify a class at runtime. Searching a phrase like "runtime bytecode manipulation" should give you some leads.

But I question whether you really want to do what you think you want to do. Can you explain why a Map<String, Integer> would not satisfy your requirement?

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82