1

Hi i have a web service in java, in which i have a class that has different methods that modify the string that the client sends and return it back according to the type of sting he wants. So based on the string type requirement a method is called..

like

class Test{

public static String a(String z)
{
 // do stuff and return;
}


public static String b(String z)
{
 // do stuff and return;
}
}

so if an array of like 10000 requests comes, these methods are called 10000 times, i want to know that should these methods be taken as static or NOT(i should create a global object of this class in my main web service class and call these methods) ?

Neel
  • 2,100
  • 5
  • 24
  • 47
Harinder
  • 11,776
  • 16
  • 70
  • 126

5 Answers5

2

I don't think you need to make method as static, if you can access the method within the object then you should (avoid static as much as possible).This improve code performance (in terms of memory utilization).

kundan bora
  • 3,821
  • 2
  • 20
  • 29
  • yes i am thinking of crating a static object of this class in my main web service class, and use that to invoke these methods? would that be ok? – Harinder May 08 '12 at 05:15
  • I think you have some confusion about static keyword. First thing is object itself can not be static (you can create static reference variable of an object). The terms static means you can acess class member directly with the reference of class (no need of object). in your scenario either you want to access method of Text class directly or you want to create an object and make its reference variable as static. let me know which option do you want ? – kundan bora May 08 '12 at 05:20
  • 1
    I suspect he means he would create a static variable in the owning class to hold an instance of the class he's writing. Perhaps create a singleton instead. – Marvo May 08 '12 at 05:21
  • @Marvo yes that is what i was saying.. so should i do that? – Harinder May 08 '12 at 05:27
  • 2
    Dennis using static reference variable in web service class will not give you any advantage.if you want a single instance of class to work on then you should use singleton pattern. you can find example here - http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html – kundan bora May 08 '12 at 05:36
  • 1
    Does the class need to maintain any state from one invocation to the next? If so, then a singleton would work, or the class-variable-in-your-containing class would work. But from your description you didn't indicate you needed to retain data between invocations. However, it would probably save on object creations, depending on implementation. – Marvo May 08 '12 at 06:11
  • @Marvo no i dont need to maintain the previous state. so i have decided not to use singleton pattern. – Harinder May 08 '12 at 06:31
  • A singleton would be worth investigating in terms of performance, I think. If you don't create a singleton, each request will need to instantiate this object. Thousands of requests, thousands of objects created and garbage collected. A singleton would create the object once, and keep it around for reuse. – Marvo May 08 '12 at 17:45
2

What's the compelling reason that these methods be static? If you need static methods, then create those. Otherwise, from a design standpoint, stick to non-static methods. You can't override static methods (though you can hide 'em.)

And there's this:

In Java, is there any disadvantage to static methods on a class?

Community
  • 1
  • 1
Marvo
  • 17,845
  • 8
  • 50
  • 74
1

You can use a static method if the method behaves the same for all cases. If the methods just does some work on the string supplied as a parameter and the behavior is the same for all instances, you can.

Again, you can also make a singleton out of it & call the methods through the UNIQUE_INSTANCE.

Something like:

public class Test {

    private static final Test UNIQUE_INSTANCE = new Test();

    private Test() {
    }

    public static final Test getUniqueInstance() {

        return UNIQUE_INSTANCE;
    }

    public final String a(String z) {

        // do stuff and return;
    }


    public final String b(String z) {

        // do stuff and return;
    }
}

Then you can do >>

Test.getUniqueInstance().a("Hello");
Test.getUniqueInstance().b("World");
Neel
  • 2,100
  • 5
  • 24
  • 47
Rahul Thakur
  • 882
  • 16
  • 31
0

If there is not any resource which is shared among the threads then there is not harm to use any static method.

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
0

Yes, there should be a Static modifier since there is no interaction between the different methods or functions, and the result is returned in the very same method. the "Static" word is to define a class variable or method that can be accessed without instantiating an object of such class.

if you do not intend to instantiate the class, and just need to use the methods inside, "Static" is the correct way to go, and set the class constructor to private.

Basilio German
  • 1,801
  • 1
  • 13
  • 22