wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instance initializer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instance initializer" Watch "instance initializer" New topic
Author

instance initializer

Geethakrishna Srihari
Ranch Hand

Joined: May 25, 2005
Posts: 30
Why is B printed twice between X and Y:

public class Test
{
int i=fun();
int j=5;
int fun(){
System.out.println("A");
return 1;}
public static void main(String[] args)
{
System.out.println("X");
Test t=new T();
System.out.println("Y");
System.out.println(t.i);
System.out.println(t.j);



}

}
class T extends Test
{
int i=fun();
int j=10;
int fun()
{
System.out.println("B");
return 2;
}
}
Chitra AP
Ranch Hand

Joined: May 25, 2005
Posts: 42
It is because you have overridden the fun() method in the subclass.

---------------
Test t=new T();
------------------

So, when you instantiate the subclass, it instantiate the parent class first. At that time it runs fun() (overridden in sub class which prints B)
and when the sub class instantiates again it prints B.

Hope it clears your doubt.
Geethakrishna Srihari
Ranch Hand

Joined: May 25, 2005
Posts: 30
Only constructors makes a call to parent constructors - Do overridden methods also follow suit?
Geethakrishna Srihari
Ranch Hand

Joined: May 25, 2005
Posts: 30
But why B is printed twice? Even if thats the case A followed by B shud be, and not B twice? Please clarify
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18506
    
  40

Originally posted by Geethakrishna Srihari:
Only constructors makes a call to parent constructors - Do overridden methods also follow suit?


An overriden method does not, by default, call the method that it is overriding. However, you can call the super class' method, by using the super keyword. (e.g. super.fun())

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18506
    
  40

Originally posted by Geethakrishna Srihari:
But why B is printed twice? Even if thats the case A followed by B shud be, and not B twice? Please clarify


The reason B is being printed twice, is because the fun() method is being called twice. Notice that there are two i variables, that you are initializing.

Henry
Shweta R Dhaneshwar
Ranch Hand

Joined: May 27, 2005
Posts: 35
Hi,
I am getting really confused here.int fun() is overriden in T(subclass)....so ,if one wants to call the fun() method from subclass T it would be resovled depending upon the type of actual object...this is run time polymorphism...right!!!...but when you want to refer the method fun() from superclass Test why does it call the sub class fun()?


Shweta Dhaneshwar.<br />SCJP 1.4 90%
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182

The above code reflects that before initialising child class instance variables it goes to initialise parent.
This occurs coz before creating the child instance , parent class is loaded as well as its instance is created !!..


Thnx
Lalitha Gottumukkula
Ranch Hand

Joined: May 24, 2005
Posts: 45
Shweta,

Although we are initializing t in the Test's class, it is the T instance we are assigning to the Test reference variable(t). So, we are instantiating the subclass.

Try with this example for more clarification.

Hope this will clear your doubt.

Thanks,
Lalitha.
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
I did not get the explanation of above code .
Can you please explain in detail?


"There are no mistakes, only lessons"
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Remembered reading this some time back

http://www.javaranch.com/maha/Discussions/Language_Fundamentals/Constructor_-_JavaRanch_Big_Moose_Saloon.htm
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
Originally posted by Amol Fuke:
I did not get the explanation of above code .
Can you please explain in detail?


Read my code... the lady who has posted after my reply has messed up the explaination
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by Nischal Tanna:

The above code reflects that before initialising child class instance variables it goes to initialise parent.
This occurs coz before creating the child instance , parent class is loaded as well as its instance is created !!..


the code by nischal tanna ..produces the following results

D:\java_prac>java Test
Inside static of parent
X
Inside static of Child
B
Inside Test
2
B
Inside Child
2
Y



can anybody pls explain the step by step..how it printed
i got most of the logic but stuck
that how 2 is printed after Inside Test

??


Thanks and Regards, Amit Taneja
Piyush Sam
Greenhorn

Joined: Jun 02, 2005
Posts: 24
2 is printed after inside Test bcoz parent class is initialize first before the child class and
{
System.out.println("Inside Test");
System.out.println(i);
}
this block of code gets executed before this code

{
System.out.println("Inside Child");
System.out.println(i);
}

and then program prints
B // int i=fun(); of child class
Inside Child // first line of fun() of child class
2 // second line of fun() of child class


Piyush Jain<br /> <br />Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
There is an excellent chapter on initialization in Bruce Eckel's Thinking in Java available free online. Should explain all you need to know about object initialization.
 
jQuery in Action, 2nd edition
 
subject: instance initializer
 
Similar Threads
this #2
Compilation Error
Int By Reference passing..
can't find the logic
Math.pow