简体   繁体   中英

Using static methods in static utility class in Java

I have a program in Java which use an helper class with static methods in the main class, as described:

public class MainClass {

  public main() {

    String abc = "xyz";
    ResultA = Helper.methodA(abc);
    ResultB = Helper.methodB(ResultA);
  }
}

and the Helper:

public class Helper { 

  public static Result methodA(String s) {
    ...
  } 

  public static Result methodB(Result r) {
    ...
  } 

}

Now, as you can see from the structure, there is a dependency of data between methodA and methodB in the helper, and I don't create any instance of class 'Helper'. Is that a proper use in static method as I have no validation of data here? Is there a better known structure for that case?

Would appreciate any help, thanks.

Better to use static methods as and when really needed. Utility classes having the static methods which share the common behaviour across the project. If your method resides in the Utility class , then go ahead. Otherwise create object and access via in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM