Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Execution of field Initializers

Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this example
class Super {
Super() { printThree(); }
void printThree() { System.out.println("three"); }

public class TestS extends Super {
int indiana = 8;
public static void main(String[] args) {
TestS t = new TestS();
void printThree() { System.out.println(indiana); }
This produces output
The first invocation of method printThree() takes
place before the execution of field initializer.
Kindly Explain ?
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subclass's constructor has implicit call to Parent class's constructor. So It invokes printThree() method. But by dynamic binding, that method points to s.o.p of the variable, which then is not initialized which means its defaulted to zero.
After that, it comes to next statement which is an explicit call to pritnThree() method. At this moment, the variable is initlaized to 8. So 8 is printed again.
- Sai
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is happening because you've overridden the method printThree in your subclass. Let's try to follow the path of execution. I've labelled some of the lines above so that we can work through this.
The first thing that will happen is the main method will start and we'll create a new TestS object at 1. In order to create that object, the parent's constructor is first called in order to ensure that the inherited state is initialized before the subclass' state is. That takes us to line 1.
In Super's constructor, we call the method printThree(). Now, this is the catch. Since we're really creating an object of type TestS, and TestS has a printThree method defined, that method is called. That takes us to line 5, not line 2. The printThree method is TestS prints the contents of the member variable indiana, which has not yet been set to 8. The default value of 0 is printed.
Now that the parent's constructor has completed, we can return to the sublcass, TestS and complete initialization. The first thing that occurs is all initialization expressions and instance initializers are executed. In this case, all we have is one initializer expression: int indiana = 8; That is executed at this time.
Now, the TestS class has been completely initialized and we can go on with the application. Line 4 is executed and printThree is called (the one at Line 5 again). This prints the value of indiana, which is now set to 8.
To make a long story short, the reason you're seeing the odd behavior is because you've overriden a method being accessed by the parent class' constructor. It's important to remember that a parent class' constructor is always executed before any instance initialization is done in the subclass.
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the steps that are gone through when a new instance is created:
1 -- evaluate the creation expression
2 -- allocate space for the new instance
3 -- create and initialize all fields of the class and its superclasses
4 -- evaluate the arguments to the constructor
5 -- invoke the appropriate constructor
after the appropriate constructor is invoked the superclass constructor is called. after it finishes, but before any other lines in the class cosntructor are executed the member fields are given their initial values. Then the rest of the constructor finishes.
For all of the details chaeck out the JLS Section 15.9.4.
So you can see the instance variable has its default value (0) before printThree is called in the superclass constructor. Then after the object is created the method is called again specifically by main and then the value of 8 is oprinted becasue it has been initialized to its given value.
hope that helps
    Bookmark Topic Watch Topic
  • New Topic