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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:03:20+00:00 2026-05-26T06:03:20+00:00

Running this piece of code will print null public class Weird { static class

  • 0

Running this piece of code will print null

public class Weird {

    static class Collaborator {
        private final String someText;

        public Collaborator(String text) {
            this.someText = text;
        }

        public String asText() {
            return this.someText;
        }
    }

    static class SuperClass {
        Collaborator collaborator;

        public SuperClass() {
            initializeCollaborator();
        }

        protected void initializeCollaborator() {
            this.collaborator = new Collaborator("whatever");
        }

        public String asText() {
            return this.collaborator.asText();
        }
    }

    static class SubClass extends SuperClass {
        String someText = "something";

        @Override
        protected void initializeCollaborator() {
            this.collaborator = new Collaborator(this.someText);
        }
    }

    public static void main(String[] arguments) {
        System.out.println(new Weird.SubClass().asText());
    }
}

(Here is also the GitHub Gist)

Now, I know why this happens (it’s because the field of the superclass is initialized and then the constructor of the superclass is called, before the field of the subclass is initialized)

The questions are:

  1. What is the design issue here? What is wrong with this design, from an OOP point of view, so that the result looks weird to a programmer? What OOP principles are being broken through this design?
  2. How to refactor so it does not work ‘weird’ and is proper OOP code?
  • 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-26T06:03:20+00:00Added an answer on May 26, 2026 at 6:03 am

    My issue with the design is the someText string should either be an explicit dependency (or “collaborator”) of the Collaborator object, or of SubClass, or explicitly a part of the global context (so a constant or a property of a shared context object).

    Ideally, either Collaborator should be responsible for retrieving its dependencies; or, if SubClass is responsible for this, it should have someText as a dependency (even if it’s always initialised to the same value), and only initialise Collaborator when someText is set.

    Conceptually speaking, the dependency relation between objects in the design imposes a partial ordering of the initialisation. The mechanism to implement this ordering should always be explicit in your design, instead of relying on implementation details of the Java language.

    An (overengineered) example:

    interface ICollaboratorTextLocator {
        String getCollaboratorText();
    }
    
    class ConstantCollaboratorTextLocator implements ICollaboratorTextLocator {
        String text;
    
        ConstantCollaboratorTextLocator(String text) {
            this.text = text;
        }
    }
    
    class SuperClass {
        Collaborator collaborator;
    
        public setCollaboratorTextLocator(ICollaboratorTextLocator locator) {
            collaborator = new Collaborator(locator.getCollaboratorText());
        }
    
        SuperClass() {
           setCollaboratorTextLocator(new ConstantCollaboratorTextLocator("whatever"));
        }
    }
    
    class SubClass {
        String text = "something";
    
        SubClass() {
            setCollaboratorTextLocator(new ConstantCollaboratorTextLocator(text));
        }
    }
    

    (Now excuse me, I need to go stand under a waterfall after writing something called ConstantCollaboratorTextLocator.)

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

Sidebar

Related Questions

Running the static analyzer on this piece of code: - (id) readForeignPref { CFPropertyListRef
I'm running this C# code in Visual Studio in debug mode: public class MyHandlerFactory
I have the same BackgroundWorker code piece in two simultaneously running applications. Will this
I am writing a piece of code which will extract words from running text.
I'd like to optimize this piece of code : public void PopulatePixelValueMatrices(GenericImage image,int Width,
I created a JAR file with my Java program. This piece of code will
I have a setInterval running a piece of code 30 times a second. This
I'm running this small C# test program launched from a pre-commit batch file private
The following piece of code tries to accompolish this. The code loops forever and
Hey, I've got this nice little piece of code, much like all the other

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.