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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:20:06+00:00 2026-05-23T19:20:06+00:00

I have wired experience with reflections. At first some sample code: public abstract class

  • 0

I have wired experience with reflections. At first some sample code:

public abstract class A {

    public A () {
        init();
    }

    public abstract void init ();
}


public class B extends A {

    private int i = 0;

    public B () {
        super();
        System.out.println(i);
    }

    public void init () {
        i = 1;
    }
}

Somewhere in my code I use the reflection api to instantiate an object B.

Class<AbstractSection> bc = (Class<AbstractSection>) Class.forName(B);
Constructor<?> bcon = bc.getConstructor();
B b = (B) bcon.newInstance();

What I expected was an instance of B with the variable i set to value ‘1’. What I got is an instance of B with i still set to ‘0’.
With a closer look with a debugger I saw that this is not exactly correct: i is NOT still set to ‘0’. It is changed to ‘1’ in
the init() method and set back to ‘0’ in the very moment when the super() call returns.

Anyone a clue? Thanks in advance,

manuel

PS: I know I can solve this by calling init() not in the super class but in the inheriting constructor.

  • 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-23T19:20:08+00:00Added an answer on May 23, 2026 at 7:20 pm

    For starters, this is nothing to do with reflection – you’d get the same results if you instantiate the class yourself.

    Your confusion likely stems from the way that the i field is defined – it looks like it’s set to 0 as soon as it “exists”. In reality, the assignment to zero is one of the first lines of your constructor (though, crucially, after the call to super() as is required by constructors in general).

    In other words, your class is exactly equivalent to the following:

    public class B extends A {
    
        private int i;
    
        public B () {
            super();
            i = 0;
            System.out.println(i);
        }
    
        public void init () {
            i = 1;
        }
    }
    

    I presume you can see now why the output is 0 instead of 1 – because the call to init() happens before the field is initialised to 0.


    It is for this, and other reasons, that on the whole you should avoid calling subclass methods from a superclass constructor – since the subclass won’t even have been initialised at this point, so invariants could easily be violated. (Calling methods on an unconstructed object is always a very bad idea!) That’s the root cause of your problem, and the direction that you should be looking to address with a fix.

    Constructors should constrain themselves to only calling methods that are private or final, for this reason. For more details, see (amongst others):

    • What’s wrong with overrideable method calls in constructors?
    • Problem in instance variable initialization
    • State of Derived class object when Base class constructor calls overridden method in Java
    • Don’t call subclass methods from a superclass constructor
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My base page looks like: namespace ASDF.Mvc.ViewEngines.Razor { public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel>
I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey.
we have a particle detector hard-wired to use 16-bit and 8-bit buffers. Every now
I have a asp.net webform with a button. The OnClientClick event is wired to
Have you guys had any experiences (positive or negative) by placing your source code/solution
I have a wired behaviour when I try to disable or readonly a input
I have successfully wired up a Facebook Connect application and everything is working fine.
I have an observableCollection and have wired up an event in MyObject, is there
I am using the jQuery autocomplete plugin and have wired up an input field
I have a class Logger which, among other things has a method Log .

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.