dcsimg
Why isn't default initialisation occurring?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Richard_Weissel
Posted On:   Sunday, December 23, 2001 06:35 PM

/** * The program currently prints "s before Super() finished is Hello" * followed by "s after Super() finished is Hello" * * If the declaration 'String s' is changed to 'String s = null' * the program prints "s after Super() finished is null" * * I know this is the wrong way to do things, but my question is * Why isn't the initialisation of instance var s set to null even if * I don't explicitly say 'String s = null' i.e. what happened to the * default initialisation to null?? * */ class Super { &nbsp&nbsp public Super() {    More>>

/**

* The program currently prints "s before Super() finished is Hello"

* followed by "s after Super() finished is Hello"

*

* If the declaration 'String s' is changed to 'String s = null'

* the program prints "s after Super() finished is null"

*

* I know this is the wrong way to do things, but my question is

* Why isn't the initialisation of instance var s set to null even if

* I don't explicitly say 'String s = null' i.e. what happened to the

* default initialisation to null??

*

*/



class Super {

&nbsp&nbsp public Super() {

&nbsp&nbsp&nbsp&nbsp init() ;

&nbsp&nbsp }



&nbsp&nbsp protected void init() {}

}



class Sub extends Super {

&nbsp&nbsp String s;



&nbsp&nbsp public Sub() {

&nbsp&nbsp&nbsp&nbsp super();

&nbsp&nbsp&nbsp&nbsp System.out.println("s after Super() finished is " + s);

&nbsp&nbsp}



&nbsp&nbsp protected void init() {

&nbsp &nbsp &nbsp&nbsp s = "Hello!";

&nbsp&nbsp &nbsp&nbsp System.out.println("s before Super() finished is " + s);

&nbsp&nbsp }

}



public class Test1 {

&nbsp&nbsp public static void main(String[] args) {

&nbsp&nbsp&nbsp&nbsp new Sub();

&nbsp&nbsp }

}

   <<Less

Re: Why isn't default initialisation occurring?

Posted By:   Christopher_Schultz  
Posted On:   Monday, December 24, 2001 09:10 AM

I think this article may have your answer:



http://www.javaworld.com/javaworld/jw-11-2001/jw-1102-java101_p.html



If you take a look at the way Java initializes instances, you'll find that even though the subclass's constructor gets called first, the superclass's constructor actually executes first. This is because the first action in a constructor must be a call to its superclass constructor.



The Java compiler executes all of the instance initialization for your class Sub after Super. executes. That means that after Super. calls Sub.init and sets s to "Hello", your Sub constructor clobbers that value with null.



The problem here is that you are making the compiler do one type of initialization (explicit init to null in your declaration), and the runtime is 'initializing' the attribute to "Hello". Due to the way the compiler handled its initialization of object instances, your runtime initialization failed.



Moral of this story: don't use declarative initializers unless you really want to declare that attribute to have that value.



Hope that helps,

-chris

Re: Why isn't default initialisation occurring?

Posted By:   Bhaskar_Goyal  
Posted On:   Monday, December 24, 2001 01:23 AM

I'm putting here the execution steps it will help u to understand the concept of problem.


1. new Sub() will call the constructor of Super class coz Sub is subclass of Super class.


2.in constructor of Super class u r calling the init() due to polimorphism it is calling the init () of Sub class in that ur s is pointing to "Hello"



regards,


Bhaskar
About | Sitemap | Contact