Novice User
Novice User

Reputation: 3824

Non-static methods and Garbage collection

I have a single validation class which has various validate methods. like

public class GlobalValidationClass {

public void validatefields(String s) {
//My Work here
}

In methods of other classes, I create an instance of the above class and then call the validatefields method.

Like

public class FirstClass {

public void firstPage() {
    GlobalValidationClass fp = new GlobalValidationClass();
    fp.validatefields("first page");
}

public void secondPage() {
    GlobalValidationClass sp = new GlobalValidationClass();
    sp.validatefields("second page");
}

My question is will it increase performance if I make the methods in my validation class static? Or it wont as the java's garbage collector will garbage collect the objects at the end of each method and there wont be any performance impact if I am following the approach of creating instance of classes in every method?

Upvotes: 1

Views: 863

Answers (5)

srikanth yaradla
srikanth yaradla

Reputation: 1235

If there is no state information associated with that validation class (no member variables) and if there is only one validation method then why not make it static (an utility class) Also when you talk about performance you are obviously avoiding the overhead of creating new objects (performace gain may be seen depending on how often these firstpage() secondPage() methods are called) and there is no need to garbage collect them either :)

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

It will help performance to use static methods, because you won't have to create objects or garbage collect them.

When there are only static methods on a class, it is called a utility class. Typically, you give it a private constructor too, to emphasize that you shouldn't create an instance.

There is another option: You could refactor your client class to minimize creation/destruction by reusing the validator:

public class FirstClass {
    // Create the validator once per client instance, 
    // instead of once per method call
    private GlobalValidationClass fp = new GlobalValidationClass();

    public void firstPage() {
        fp.validatefields("first page");
    }

    public void secondPage() {
        sp.validatefields("second page");
    }

Upvotes: 1

arya
arya

Reputation: 170

You can use a static method if you do not any particular object state. Creating a object every time and then invoking the method which does the same thing is a perfect use-case for replacing it with a static method. Its a more scalable solution.

Upvotes: 0

Harmeet Singh
Harmeet Singh

Reputation: 2616

It will increase performance if you make the methods in my validation class static but on the other side there will be memory hike(depending upon static methods and variables) as static get memory once your program starts till the end of your program.

and get stats using jvisualvm, its nice tool, by default its in /jdk/bin/

Upvotes: 4

hertzsprung
hertzsprung

Reputation: 9893

Firstly, have you identified this code as being a performance bottle neck? Remember, you shouldn't prematurely optimize!

Realistically, I wouldn't expect you to see much performance gain by making a method static. But the only way to find out is to measure it.

Upvotes: 0

Related Questions