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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:31:39+00:00 2026-05-25T16:31:39+00:00

I recently spent quite a few minutes debugging a problem in production code that

  • 0

I recently spent quite a few minutes debugging a problem in production code that in the end turned out to be caused by a class calling an abstract method in its constructor, and the subclass implementation of that method tried to use a subclass field that had not been initialized yet (An example that illustrates the point is included below)

While researching this, I stumbled across this question, and was intrigued by Jon Skeet’s answer:

In general it’s a bad idea to call a non-final method within a constructor for precisely this reason – the subclass constructor body won’t have been executed yet, so you’re effectively calling a method in an environment which hasn’t been fully initialized.

This has me wondering, is there ever a legitimate reason to call a non-final or abstract method from a constructor? Or is it pretty much always a sign of bad design?

Example

public class SSCCE {
    static abstract class A {
        public A() {
            method();  // Not good; field arr in B will be null at this point!
        }

        abstract void method();
    }

    static class B extends A {
        final String[] arr = new String[] { "foo", "bar" };

        public B() {
            super();
            System.out.println("In B(): " + Arrays.toString(arr));
        }

        void method() {
            System.out.println("In method(): " + Arrays.toString(arr));
        }
    }

    public static void main(String[] args) {
        new B().method();
    }
}

And here is expected output:

In method(): null
In B(): [foo, bar]
In method(): [foo, bar]

The problem, of course, is that in the first call to method() the field arr is null because it hasn’t been initialized yet.

  • 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-25T16:31:40+00:00Added an answer on May 25, 2026 at 4:31 pm

    There are times it can be very hard not to.

    Take Joda Time, for example. Its Chronology type hierarchy is very deep, but the abstract AssembledChronology class is based on the idea that you assemble a bunch of “fields” (month-of-year etc). There’s a non-final method, assembleFields, which is called during the constructor, in order to assemble the fields for that instance.

    They can’t be passed up the constructor chain, because some of the fields need to refer back to the chronology which creates them, later on – and you can’t use this in a chained constructor argument.

    I’ve gone to nasty lengths in Noda Time to avoid it actually being a virtual method call – but it’s something remarkably similar, to be honest.

    It’s a good idea to avoid this sort of thing if you possibly can… but sometimes it’s a real pain in the neck to do so, especially if you want your type to be immutable after construction.

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

Sidebar

Related Questions

I recently spent a long while debugging something that turned out to be an
I recently spent a great deal of time pulling out a stored procedure back-end
Recently I spent quite some time writing various Visual Studio Extensions projects. Even though
I spent a lot of time recently reading about debugging. One of the aspects
I inherited some code that was recently attacked where the attacker sent repeated remote
I am working on a Vehicle Routing Problem. Recently I have spent a bit
Having spent a bunch of time recently doing 2D work that involves pixel manipulation
I'm in the process of learning Scheme. I recently spent (too much!) time trying
Recently my team has spent too many hours setting up machines with previous product
I was recently working on an application that sent and received messages over Ethernet

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.