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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:47:58+00:00 2026-05-15T23:47:58+00:00

Please refer to the Java code below: class Base{ Base(){ System.out.println(Base Constructor); method(); }

  • 0

Please refer to the Java code below:

class Base{
     Base(){
         System.out.println("Base Constructor");
         method();
     }
     void method(){}    
}

class Derived extends Base{
    int var = 2;
    Derived(){
         System.out.println("Derived Constructor");  
    }

     @Override
     void method(){
        System.out.println("var = "+var);
     }
 }

class Test2{
    public static void main(String[] args) {
        Derived b = new Derived();
    }
}

The output seen is:

Base Constructor
var = 0
Derived Constructor

I think var = 0 occurs because Derived object is half initialized; similar to what Jon Skeet says here

My questions are:

Why does the overridden method get called if the Derived class object isn’t created yet?

At what point in time is var assigned value 0?

Are there any use cases where such behavior is desired?

  • 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-15T23:47:58+00:00Added an answer on May 15, 2026 at 11:47 pm
    • The Derived object has been created – it’s just that the constructor hasn’t been run yet. The type of an object never changes in Java after the instant it is created, which happens before all constructors run.

    • var is assigned the default value of 0 as part of the process of creating an object, before constructors are run. Basically, the type reference gets set and the rest of the memory representing the object gets wiped to zero (conceptually, anyway – it may already have been wiped to zero before, as part of garbage collection)

    • This behaviour at least leads to consistency, but it can be a pain. In terms of consistency, suppose you had a read-only subclass of a mutable base class. The base class may have an isMutable() property which was effectively defaulted to true – but the subclass overrode it to always return false. It would be odd for the object to be mutable before the subclass constructor ran, but immutable afterwards. On the other hand, it’s definitely strange in situations where you end up running code in a class before the constructor for that class has run 🙁

    A few guidelines:

    • Try not to do much work in a constructor. One way of avoiding this is to do work in a static method, and then make the final part of the static method a constructor call which simply sets fields. Of course, this means you won’t get the benefits of polymorphism while you’re doing the work – but doing so in a constructor call would be dangerous anyway.

    • Try very hard to avoid calls to non-final methods during a constructor – it’s very likely to cause confusion. Document any method calls you really have to make very clearly, so that anyone overriding them knows that they will be called before initialization has finished.

    • If you have to call a method during construction, it’s usually not then appropriate to call it afterwards. If that’s the case, document it and attempt to indicate it in the name.

    • Try not to overuse inheritance in the first place – this is only going to become an issue when you’ve got a subclass deriving from a superclass other than Object 🙂 Designing for inheritance is tricky.

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

Sidebar

Related Questions

Please refer to the following java source code : static class SynchronizedList<E> extends SynchronizedCollection<E>
By using the following code. BuyActivity.java public class BuyActivity extends ListActivity { public void
Please refer to the below jsfiddle code snippet: http://jsfiddle.net/JLXs5/3/ When the user clicks here,
Please refer to my code below. When optimization in IAR MSP430 compiler is set
Please refer to code. <%= form_tag(:action => create_user, :method => post) do%> <p><label for=first_name>First
Please refer the below code. I want the boxes for Blue and all other
Please refer code below i m learning jquery and i want to display tag
please let me know a java multithread application which i can refer ( source
Please refer to the image below - I'm not using NavigationViewController. Is there a
Please refer my previous question for code sample Sockets: sometimes (rarely) packets are lost

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.