jack_carver
jack_carver

Reputation: 1526

Java static method

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

Upvotes: 2

Views: 1782

Answers (7)

Amos M. Carpenter
Amos M. Carpenter

Reputation: 4958

To get a basic understanding of static variables/methods versus instance variable/methods, read this tutorial or search for similar ones. Also, this question and its answers should help explain when exactly methods are thread-safe (yours doesn't modify any data that could be shared between threads, so it's perfectly safe).

As for your code, your method signature should look something like this:

public static List<List<String>> test(List<String> input) {...}

since you'll be returning a list of lists of strings.

Upvotes: 1

Jaco Van Niekerk
Jaco Van Niekerk

Reputation: 4182

I agree, the method is perfectly fine.

Just be careful though, the method may be thread safe, but the List that you pass in, is not. If you (for example) iterate over the list, while another thread is inserting Strings, you may run into some problems. Strings, being immutable are thread-safe of course.

Upvotes: 2

Jwalin Shah
Jwalin Shah

Reputation: 2521

Here while declaring method you need to write return type of method.

for example :

 import java.util.List;

    class Test
    {
        public static List test(List<String> input) {
            List<List<String>> res = new ... ;
            // some code
            return res;
        }
    }

So, this method will return list and for static method you do not need to create object of that class

Upvotes: 2

gprathour
gprathour

Reputation: 15333

Static method can access only static objects.

So if you will be using any object in your static method that is also going to be static so having same value for all threads.

So you can easily do this.

Upvotes: 1

Matthias Meid
Matthias Meid

Reputation: 12513

This is okay, basically, as long as you are working with local variables only. Thus, your code sample, where you create a new List each time you invoke the method. Whether this is an elegant design or not is a different story.

If you use a state, e.g. some member fields, this is a different story. Multiple threads might invoke your method, and they work on the very same objects.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240860

Static method is associated with class if you call it from different thread there won't be any problem for your demonstrated code since res would be thread local

Upvotes: 2

Petar Ivanov
Petar Ivanov

Reputation: 93020

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.

Upvotes: 4

Related Questions