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 8283595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:47:19+00:00 2026-06-08T10:47:19+00:00

I got a class Foo having a method doSomething that uses two class-related variables

  • 0

I got a class Foo having a method doSomething that uses two class-related variables b and c that are expensive to get/create. My first version looks like this:

public class Foo {

    private final A a;

    public Foo(A a) {
        this.a = a;
    }

    public void doSomething() {
        final B b = a.getB();
        final C c = b.getC();

        for (int i = 0; i < 1000; i++) {
            // do something with b and c
        }
    }
}

So I get the first object (type B) via a class variable a and the second object (type C) via the first object.

Now, since those variables are related to the class and the method is always called exactly one time (though not necessarily when creating an object of type Foo), I thought about making them class variables as well:

public class Foo {

    private final A a;
    private final B b;
    private final C c;

    public Foo(A a) {
        this.a = a;
        b = a.getB();
        c = b.getC();
    }

    public void doSomething() {
        for (int i = 0; i < 1000; i++) {
            // do something with b and c
        }
    }
}

I’m not sure which version to use if any of those two. I somehow don’t feel comfortable making those two variables class members since they can be retrieved from the existing class variable a. However, it would increase readability of the methods IMO.

  • 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-06-08T10:47:21+00:00Added an answer on June 8, 2026 at 10:47 am

    You are absolutely right. If it increases readability, bu all means do it. However, I would ask you this: What is the purpose of Referencing A from within the class? Is it only for getting B and C?
    In this case, I would just input B and C in Foo’s constructor!

    This way you even make it more readable by breaking the dependency on A and Making the dependency on B and C more explicit.

    Also, consider whether you are using these variables in other methods in the class. If the answer is yes- it signals that they should be class members, However, if the class contains a lot of methods that do not use these variables, that might signal the opposite.

    The general principle you should follow here is the principle of High Cohesion

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a class Foo<T> . How can I say that I want T
Let's say that I've got a BarService under grails-app/services and regular Groovy class 'Foo'
I've got a class foo, a class that derives from food called bar, and
Say we've got the following two classes: abstract class Foo { public abstract function
I've got a class Foo with a property Id . My goal is that
I've got a template class with a template method within it, giving two template
Relatively simple question about handling destructors properly... First I've got a class that's something
I've got a method that creates some Foo and adds it to a vector
I've got a class Foo , it's Serializable . It represents a graphical object,
I've got a class which uses an XmlSerializer in its Read/WriteXml methods. The Serializer

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.