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

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

8 Comments

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?
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 ?
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.
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 - javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
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.
|
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?

1 Comment

would have accepted your ans also if accepting two were allowed.. thnks for ur help.. :)
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");

Comments

0

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.