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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:40:34+00:00 2026-05-27T01:40:34+00:00

public class ParentClass { public ParentClass(int param); } public class MyClass extends ParentClass {

  • 0
public class ParentClass
{
    public ParentClass(int param);
}

public class MyClass extends ParentClass
{
    private int _a;
    private int _b;
    private int _c;

    public MyClass(String input)
    {
        _a=CalculateA(input);
        _b=CalculateB(_a);
        _c=CalculateC(_a);
        super(_b+_c);
    }

    //a expensive procedure
    private int CalculateA(String text);

    private int CalculateB(int a);
    private int CalculateC(int a);  
}

Java doesn’t allow chained constructors to be anything other than the first method put in a constructor.
Chained constructors can’t call nonstatic methods as arguments (which removes the possibility of using Initialsers that return the value they initialize to).

How do I achieve the above code using legal Java?

  • 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-27T01:40:35+00:00Added an answer on May 27, 2026 at 1:40 am

    Edit Indeed Java does not allow a constructor to do any calculations before the call to a parent’s class constructor, even if these involve only static methods (as your calculateX‘s should be) and results only assigned to variables that are private to the class (like your _a, _b and _c) or local to the constructor.

    There is a way around this, however: call another constructor with the result of the calculateX call assigned to its parameter – then you can access this result throughout the other constructor.

    public class MyClass extends ParentClass {
        private int _a,_b,_c;
    
        public MyClass(String input) {
            this(calculateA(input));
        }
    
        private MyClass(int a) {
            this(a, calculateB(a), calculateC(a));
        }
    
        private MyClass(int a, int b, int c) {
            super(b + c);
            this._a = a;
            this._b = b;
            this._c = c;
        }
    
        private static int calculateA(String text) {
            try {Thread.sleep(1000);} catch (Exception e) {}  // expensive ;-)
            return text.length();
        }
    
        private static int calculateB(int a) { /* ... */ }
        private static int calculateC(int a) { /* ... */ }
    }
    

    Edit 2 With more calculations or more intermediate results to store for later use, this approach would lead to an even longer chain of constructors consisting only of this(...)-calls. A more fancy solution with only two constructors, the public one and one private, is possible with a helper class (reasonably an inner class):

        public MyClass(String input) {
            this(new InitCalcResult(input));
        }
    
        private MyClass(InitCalcResult initCalcResult) {
            super(initCalcResult.initB + initCalcResult.initC);
            this._a = initCalcResult.initA;
            this._b = initCalcResult.initB;
            this._c = initCalcResult.initC;
        }
    
        private static class InitCalcResult {
            private int initA, initB, initC;
    
            InitCalcResult(String input) {
                initA = calculateA(input);
                initB = calculateB(initA);
                initC = calculateC(initA);  
            }
        }
    

    (using the same private fields and static calculateX methods as above).

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

Sidebar

Related Questions

public class MyClass { public int Age; public int ID; } public void MyMethod()
my child class is public class User extends Alien { public User(XYCoordination currentLocation, int
Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS
public class Test { public static void main(String[] args) { } } class Outer
public class Address { public string ZipCode {get; set;} } public class Customer {
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { static { print(10); } static void print(int x) { System.out.println(x);
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
My Parent class is : import java.io.IOException; public class Parent { int x =

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.