I am new to java and I wanted to know if is ok to return an object from a static method ?
Since static methods operate on a class … I am a little confused here.
Also what if multiple threads are trying to call this ?
class Test
{
public static test(List<String> input) {
List<List<String>> res = new ... ;
// some code
return res;
}
}
There is no problem returning an object from static method.
Your sample is also fine with multiple threads. Each thread will create a different object on the heap and return a reference to it – so no problems, the object is not shared.
Static methods do NOT operate on a class. They are just bound to class instead of to member of that class. This means that they don’t have access to any non static members of the class. Other than that, they are not very different than the non-static methods.
If your static method accessed (write or read) a static member, then it might possibly has problems with multiple threads, unless you use locking.