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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:46:04+00:00 2026-06-10T21:46:04+00:00

When does the instance variable get initialized? Is it after the constructor block is

  • 0

When does the instance variable get initialized? Is it after the constructor block is done or before it?

Consider this example:

public abstract class Parent {

 public Parent(){
   System.out.println("Parent Constructor");
   init();
 }

 public void init(){
   System.out.println("parent Init()");
 }
}

public class Child extends Parent {

private Integer attribute1;
private Integer attribute2 = null;

public Child(){
    super();
    System.out.println("Child Constructor");
}

public void init(){
    System.out.println("Child init()");
    super.init();
    attribute1 = new Integer(100);
    attribute2 = new Integer(200);
}

public void print(){
    System.out.println("attribute 1 : " +attribute1);
    System.out.println("attribute 2 : " +attribute2);
}
}

public class Tester {

public static void main(String[] args) {
    Parent c = new Child();
    ((Child)c).print();
    
}
}

OUTPUT:

Parent Constructor

Child init()

parent Init()

Child Constructor

attribute 1 : 100

attribute 2 : null


  1. When are the memory for the atribute 1 & 2 allocated in the heap ?

  2. Curious to know why is attribute 2 is NULL ?

  3. Are there any design flaws?

  • 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-06-10T21:46:06+00:00Added an answer on June 10, 2026 at 9:46 pm

    When the memory for the atribute 1 & 2 are allocated in the heap ?

    The memory for the object as a whole is allocated when the new operator is invoked, before the java.lang.Object constructor is entered. Memory is allocated for individual Integer instances in init, but there is no point when memory is allocated for individual properties — only whole objects.

    Curious to know why is attribute 2 is NULL ?

    The init method is called in the super constructor, so attribute2 is assigned new Integer(200), and then the subclass constructor is invoked which applies property initializers in the order they appear in the source code. This line

    private Integer attribute2 = null;
    

    overwrites the value assigned by init() to null.

    If you add a call to

     System.out.println("attribute 2 : " +attribute2);
    

    right after your call to super(); then this will become apparent.

    Are there any design flaws?

    Calling sub-class methods before the base class has finished initializing is dangerous. The sub-class might rely on its base-class’s invariants to protect its own invariants, and if the base-class constructor has not completed, then its invariants may not hold.

    This is also likely to confuse C++ programmers and the like who would expect a call to init from the base class to invoke the base class’s version since C++ rewrites the vtable pointer as constructors are entered.

    See The Java Language Specification for all the gory details.

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

Sidebar

Related Questions

Does startService() create a new Service instance or using the existing one? For example,
How many bytes of data does a typical HTTP get request consume. For instance
I have an instance variable which is a struct, for example: struct Data {
For example this class: class RTPIPv4Address{ public: RTPIPv4Address(int a, int b); } Silly question
Do variables declared in methods get erased after the method call is done? Ie
When I set or get instance variables using some name, for example @foo ,
I am running a thread like this: instance variable private Thread workerThread = null;
The Question: When you UnicastRemoteObject.exportObject(instance) . Does that instance now become publicly available to
1.Does dynamic proxy instance subclass the target class? The java doc says the proxy
Does the entrySet() function that is called from a treemap instance return a TreeSet

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.