Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:29:09+00:00 2026-05-26T22:29:09+00:00

Say I have this class: class MyClass { private String s; // more attributes

  • 0

Say I have this class:

class MyClass {
    private String s;
    // more attributes here
    public MyClass(String s, /*more constructor params*/)  {...}

    public String myMethod(String s) {
        //complex logic here
    }
}

To unit test myMethod() I need to create the entire object (with many parameters that need constructed, etc), while the method only uses s.

Altenatelly I can add a static method:

class MyClass {
    private String s;
    // more attributes here
    public MyClass(String s, /*more constructor params*/)  {...}

    public String myMethod(String s) {
        return myStaticMethod(s);
    }

    public static myStaticMethod(String s) {
        //complex logic here
    }
}

Now I can easily test “complex logic” without the need to create the object.
someStaticMethod(String s) should have no side-effects on the class.
So I am adding an extra method just for the ease of testing.
Is this a good practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:29:10+00:00Added an answer on May 26, 2026 at 10:29 pm

    So, you’ve made a complex method a member of an object even though it has nothing little to do with that instance?

    Yes, I agree you should use a different design. It could be a static method in that class, or factored into its own class. Or it might be a method of an object that implements a “Strategy” pattern. The right decision depends on the potential for change.


    Maybe something like this:

    class ComplexLogician {
    
      String myMethod(String a, String b) {
        /* Complex logic here. */
      }
    
    }
    
    class MyClass {
    
      private String s;
    
      private final ComplexLogician logic;
    
      /* More attributes here... */
    
      MyClass(String s, ComplexLogician logic, /* More parameters... */)  {...}
    
      String myMethod(String b) {
        return logic.myMethod(s, b);
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say I have a class like this: public class MyClass { private String
Let's say, I have this class public class MyClass { public int MyMethod(int? i)
let's say i have this public class MyClass { public string myMessage { get;
Say I have this class: class myclass { public int Field1{ get; set; }
Let's say we have a class: public class MyClass { @Autowired private AnotherBean anotherBean;
lets say I have the following public class A { private string _someField; public
Let's say I have the following class: class MyClass { private: int Data; public:
Let's say I have a Java class like this: public class MyClass { public
Say I have this class: public class Account { public int AccountID { get;
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.