Get the variable by its name [duplicate] - java

This question already has answers here:
Get variable by name from a String
(6 answers)
Accessing the value of a variable by its name as string in Java
(2 answers)
Closed 5 years ago.
How can I access the variable value by providing the String that contains name of that variable?
Like this:
int var1 = 123;
getVariableByName("var1");
It should return 123
I know that I can create a HashMap or something...
But can I do this shortly, without a couple of code?
I need to create some objects with similar data, so I did this:
(I'm working with android)
String string2="bla";
String string2="bla-bla";
String string3="bla-bla-bla"
for(int i=1; i<=2; i++){
TextView tv = new TextView;
TextView.setText(getValueOf("string"+i));
}
Just it isn't smart to make a huge HashMap if you need to create a lot of objects. There should be a way to do it optimised...

You can't do that with local variables in Java. Local variables are variables you define inside a method. The names do not survive compilation step and are not available at runtime.
You can lookup fields via reflection via Foo.class.getField or obj.getClass().getField

Related

How to manipulate variables in a method and send it back [duplicate]

This question already has answers here:
How do I return a value from a Java function?
(3 answers)
returning a value from a method to another method
(8 answers)
Using a return value from a method in another method
(5 answers)
How do I use the "return" value of one method in another method
(2 answers)
Closed 11 months ago.
I've seen a few questions about this but I haven't been able to find the answer yet. So I'm basically been trying to have everything based inside methods and only have the main full of calls to the methods but, I'm running into problems trying to actually manipulate data, pass them through the methods and save it into main. This isn't the actual code I'm trying to write but this pretty much encapsulates my problem.
enter image description here
Methods cannot manipulate input parameters, you will need to declare return values for your methods. Like this:
private static int getArea(int width, int length) {
return width * length;
}
And then call it like this:
area = getArea(width, length);
This is pretty basic stuff, you should go read some introduction to Java programing, such as the oracle java tutorial.

Can you declare two variables of different types in one statement? [duplicate]

This question already has answers here:
Initialize multiple variables at the same time after a type has already been defined in Java?
(3 answers)
Closed 6 years ago.
A question from a publicly disclosed exam read "Write a single statement that declares two variables, a and b, and initializes them with the values 7.3 and "Goodbye". "
The answer in the key:
double d = 7.3; //or float f = 7.3; (they must have 7.3f, but we will accept 7.3)
String message = “Goodbye”;
Is this a typo? That looks like two statements to me, unless if I have mixed up the definition of a statement.
Write a single statement that declares two variables, a and b, and initializes them with the values 7.3 and “Goodbye”. (Choose an appropriate type.)
From here. Based on 'Choose an appropriate type', I'd say:
private String a = "7.3", b = "Goodbye";

Store a method's name in a variable and call that method using that variable [duplicate]

This question already has answers here:
How do I invoke a Java method when given the method name as a string?
(23 answers)
Closed 6 years ago.
I want to call a method whose name (with or without parentheses and parameters) is stored in a string variable and call this method using this variable.
Until now I have found that in Java if you do this:
String str = "func();";
System.out.println(str);
This would call method func(). But I don't think this is recommended.
So, my question is:
Is it okay to do this?
Is there any other way to do this?
Thanks.
Method m = YourClass.class.getMethod("methodName", parametersOfYourMethods);

Java: get the variable name from a variable [duplicate]

This question already has answers here:
Java Reflection: How to get the name of a variable?
(8 answers)
Closed 7 years ago.
I'm facing following problem: I have to give names to many thousands of GUI-Elements. Would be nice if it could work a script like:
JMenuItem myMenuItem = new JMenuItem();
myMenuItem .setName(this.getClass().getSimplename() + "." + myMenuItem .get??)
Would be nice if I could set the name of the element to "Classname.myMenuItem".
Is there any way to get the name (variable name ofc, not JMenuItem.getName()) of the variable?
You can do this for field via reflection: see Field.getName().
You cannot do it for local variable, they basically don't have any name after the code is compiled (well, in debug info they have but it's not easily accessible and understandable unless you implement debugger :-) ).
What you want is not possible. Even if some information about variable names is present in the class file, you still have to remember that there is a difference between the variable and the object it refers to. There can be any number of variables (local variables, member variables, parameters, etc) that refer to the same object. Keeping track of the names of all of those variables would impose too much of a run-time overhead.

Java / Get all arguments in reflection [duplicate]

This question already has answers here:
Can I obtain method parameter name using Java reflection?
(15 answers)
Closed 9 years ago.
I search way to get all the names of a function in java. for example:
public String doSomething(String user, boolean tree, String[] arr){
// HERE I want to get all the names of the params, meaning: user, tree and arr, and do something with them
}
How can I do it?
You can, but only if you have debugging information in the code. The names of these variables exist mainly for you, not for the processor.
Details here:
Can I obtain method parameter name using Java reflection?

Categories

Resources