Java / Get all arguments in reflection [duplicate] - java

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?

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.

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);

Can't call createTempDirectory in Clojure [duplicate]

This question already has answers here:
How to handle java variable length arguments in clojure?
(3 answers)
Closed 8 years ago.
Code in clojure:
(import '(java.nio.file Files))
(Files/createTempDirectory "Test")
There is error:
CompilerException java.lang.IllegalArgumentException: No matching method: createTempDirectory, compiling:xxxx
But in java's doc http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempDirectory(java.lang.String,%20java.nio.file.attribute.FileAttribute...)
There is an String parameter for createTempDirectory, I'm using java 1.7.0
Try this code:
(java.nio.file.Files/createTempDirectory "Test"
(into-array java.nio.file.attribute.FileAttribute []))
As #ymonad mentioned, you cannot omit the variable argument when calling java method with variable arguments. If you don't want to specify the FileAttribute, just pass the empty array of the type.

Calling method from a component by its name in a string [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 10 years ago.
I just want to know if there is any way to call a method from on an object by its name in a String.
Something like this
setLocation(int,int) it's a method from jLabel1, how can i call this
method(setLocation()) by using its name in string ?
String component = jLabel1.getName();
component.setLocation(x,y);
Sure, you can use the Java reflection API
This SO question may helps you.

When sent to a constructor in Java, what does "null" value do? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Which constructor is chosen when passing null?
I recently came across this curiosity while coding a few days back and can't seem to figure out why the following happens:
Given the class below
public class RandomObject{
public RandomObject(Object o){
System.out.println(1);
}
public RandomObject(String[] s){
System.out.println(2);
}
}
When the call new RandomObject(null); is made the output is always 2 regardless of the order in which the constructors were created. Why does null refer to the string array rather than the object?
The key here is that Object is the super type of String[]
Java uses the most specific available method to resolve such cases. Null can be passed to both methods without compilation errors so Java has to find the most specific method here. The version with String[] is more specific - therefore it will be chosen for execution.
Someone else has had this question earlier, check this post
If there are two cases to choose from, the compiler will first try to pick the more specific case. In this case, String will be picked over Object.
In the other question it was String str instead of String[] s
Thus, since String[] is a more specific datatype than its super type Object, it is picked.

Categories

Resources