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.
Related
This question already has answers here:
Cannot make a static reference to the non-static method
(8 answers)
Closed 8 years ago.
I am new to programming in Java, and new to apach.commons.math3 library.
I want to use fft in order to transform time series into Fourier series.
the time series is saved in array of double named input.
I'm using the following line to call the transform function:
Complex[] fourierSereis=FastFourierTransformer.transform(input,TransformType.FORWARD);
ButI get the following error:
Cannot make a static reference to the non-static method
transform(double[], TransformType) from the type
FastFourierTransformer
Can someone please explain me what does that error means and what should I do to make it work?
Thank you
You have to create an object of FastFourierTransformer type and then call this method because it's not a static method of this class.
This question already has answers here:
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 9 years ago.
I am currently trying to get AsyncTasks working in Android, and have found something I have never seen before over and over again in many different tutorials.
Certain methods in the tutorials are passed parameters that look like this String... arg0, Integer... values.
Here is a tutorial with some code shown similar to what I am describing.
What does this mean? Why is the ... there?
It is called varargs. It works for any type as long as it's the last argument in the signature.
Basically, any number of parameters are put into an array. This does not mean that it is equivalent to an array.
A method that looks like:
void foo(int bar, Socket baz...)
will have an array of Socket (in this example) called baz.
So, if we call foo(32, sSock.accept(), new Socket()) we'll find an array with two Socket objects.
Calling it as foo(32, mySocketArray) will not work. However, if the signature is a varargs of arrays you can pass one or more arrays and get a two-dimensional array. For example, void bar(int bar, PrintStream[] baz...) can take multiple arrays of PrintStream and stick them into a single PrintStream[][].
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
How to return multiple objects from a Java method?
(25 answers)
Closed 9 years ago.
I'm a new comer form C#, and I know clearly that "Java is always pass-by-value."
But pass-by-reference is useful when we want to get multiple outputs from one method.
How can we get multiple outputs from one method in java, as in C#.
I know one way to do this -- use a generic wrapper class, and get value from the field.
class Wrapper<T> {
public Wrapper(T value) {
Value = value;
}
public T Value;
}
Is there another way to realize this effect?
No, Java does not have out parameters. You can pass an object reference that the method is to modify to pretend that it has out parameters, but this isn't usually the best design and runs into other issues (multithreading and mutable state for one).
The best way to achieve a method that returns multiple values is to have the method return a type that contains multiple values.
Another way to simulate call by reference in Java is to pass a one-element array as a parameter.
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.
This question already has answers here:
Getting the name of the currently executing method
(23 answers)
Closed 8 years ago.
I'm looking for a way to get the name of the current method without having to create a blank object. Is there a way to do this? This would tidy up our logging code.
Here is what we do now:
new Object() {}.getClass().getEnclosingMethod().getName(
How about Thread.currentThread().getStackTrace()[1]?
Since this enables you to examine higher levels of the stack trace, you could easily wrap this in a helper method (see below). It also gives you the option to get quite a bit more info than just the method name, such as the file name, line number etc.
edit The helper method could look something like this (thanks #Esailija):
public static String getMethodName() {
return Thread.currentThread().getStackTrace()[2].getMethodName();
}
You can also use
Thread.currentThread().getStackTrace()[1].getMethodName()
contains the last method call, but is not shorter that
new Object() {}.getClass().getEnclosingMethod().getName
I am not aware of a sorter way to do it.
You can use thread.currentThread().getStacktrace()[0].getMethodName(). But this even takes more time than new Throwable().getStacktrace() (see http://alexradzin.blogspot.co.il/2011/12/how-slow-getstacktrace-is.html)