• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

instance initializer

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only constructors makes a call to parent constructors - Do overridden methods also follow suit?
 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why B is printed twice? Even if thats the case A followed by B shud be, and not B twice? Please clarify
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()?
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !!..
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not get the explanation of above code .
Can you please explain in detail?
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remembered reading this some time back

http://www.javaranch.com/maha/Discussions/Language_Fundamentals/Constructor_-_JavaRanch_Big_Moose_Saloon.htm
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

??
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic