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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:16:09+00:00 2026-06-09T03:16:09+00:00

I have class A { int var; public A(int x) { var = x;

  • 0

I have

class A {

    int var;
    public A(int x) {
        var = x;
    }
}


class B extends A {
     int var2;

     public B(int x, int y) {
         super(...);
         var2 = y;
         x = f(y);
     }
 }

For the subclass B, I need to calculate the value x that is used in the constructor of A. If I were free to move super below my x=f(y) then I could pass in the result to the constructor of A (super). But super has to be the first line in the constructor of B.

Is there any way to initialize A with the proper value the first time? What if A.var were final and i couldn’t go back and change it after construction?

Sure, I could put super(f(y)), but I could imagine cases where this would become difficult.

  • 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-09T03:16:11+00:00Added an answer on June 9, 2026 at 3:16 am

    Assuming var is private and you need to set the value with the constructor (which seems to be the point of the question, otherwise there are many easy solutions), I would just do it with a static factory-like method.

    class B extends A {
         int var2;
    
         public static B createB(int x, int y) {
             x = f(y);
             return new B(x, y);
         }
    
         public B(x, y) {
             super(x);
             this.var2 = y;
         }
     }
    

    something like that. You have no choice, as explicit constructor invocation must happen on the first line of the wrapping constructor.

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

Sidebar

Related Questions

I have class A such that: class A { static int i; A(); f1();
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I new to flex, I have a class shown below: public class Items extends
I have a class (ImageLoader) that extends Sprite container and loads an external image
I have an abstract class like this public abstract class Abstractclass { public Abstractclass(int
I have class Person class person{ int ID ; sting FirstName ; string Last
I have: class Foo { int a; int b; std::string s; char d; };
assume we have : class Dictionary { int n; int *ints; char **strs; inline
if i have class foo { int a int b } and a List<foo>
For example can I have class Dog { int legs; Bone chewy; Bone squeeky;

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.