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

  • SEARCH
  • Home
  • 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 8638927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:51:57+00:00 2026-06-12T10:51:57+00:00

Is there a good way to maintain a reference to an object created in

  • 0

Is there a good way to maintain a reference to an object created in a constructor that needs to be passed to the super constructor of some extended class (other than making it accessible from the super class or passing it into the constructor as a parameter)?

Let me clarify with an example. Take this class (which I can’t modify and which doesn’t give me access to foo):

class TestA {
    TestA(Object foo) {}
}

Now, I would like to extends this class as follows:

class TestB extends TestA {
    Object myCopyOfFoo;

    TestB() {
        super(new Object());
    }
}

Is there a good way to store the created new Object() in myCopyOfFoo?

Neither one of these three ideas work:

TestB() {
    myCopyOfFoo = new Object();
    super(myCopyOfFoo);
}

(Error: Constructor call must be the first statement in a constructor)

TestB() {
    super(myCopyOfFoo = new Object());
}

(Error: Cannot refer to an instance field myCopyOfFoo while explicitly invoking a constructor)

TestB() {
    super(makeFoo());
}

Object makeFoo() {
    myCopyOfFoo = new Object();
    return myCopyOfFoo;
}

(Error: Cannot refer to an instance method while explicitly invoking a constructor)

I guess I could do the following, but it’s neither thread-safe nor elegant:

static Object tempFoo;

TestB() {
    super(tempFoo = new Object());
    myCopyOfFoo = tempFoo;
}

Does anyone have a better idea for me? And why on earth are my first two ideas not legit?

  • 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-12T10:51:58+00:00Added an answer on June 12, 2026 at 10:51 am

    How about:

    class TestB extends TestA {
        Object myCopyOfFoo;
    
        // I assume you actually wanted this to take a parameter called foo?
        // I've left it as per the question...
        TestB() {
            this(new Object());
        }
    
        private TestB(Object copy) {
            super(copy);
            myCopyOfFoo = copy;
        }
    }
    

    As the second constructor is private, it can only be called within the same class (or an enclosing class) so you just need to make sure that anything calling the second constructor has made an appropriate copy, as the first constructor does.

    EDIT: If your real situation is that you’re taking a copy of an existing parameter, then this would work…

    class ClassB extends ClassA {
        private final Foo copyOfInput;
    
        ClassB(Foo input) {
            super(input = input.clone());
            copyOfInput = input;
        }
    }
    

    It’s pretty ugly though 🙁

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

Sidebar

Related Questions

Is there a good way to get the debug information that Django provides when
Is there a good/right way to maintain/administrate the internationalization of a Wicket application? At
Is there a good way to keep keys from conflicting when using the Microsoft
Is there a good way to start at a given index and move BACKWARDS
Is there any good way of truncating text with plain HTML and CSS, so
Is there a good way in asp.net MVC to trace ModelState errors? IsValid returns
Is there a good way in C# to mimic the following python syntax: mydict
Is there a good way to test whether I am logging into a text
Is there a good way to debug exited with code 5 runtime error on
Is there a good way to keep consistency in the $_GET For example if

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.