Connecting Tech Pros Worldwide Forums | Help | Site Map

plz explain this ?????

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#1: Mar 20 '07
how can i explain this .........

class S1
{
static void Static()
{
System.out.println("Here i m ...");
}
}

class S2 extends S1
{
void test()
{
Static();
}
}

class StaticTest1
{
public static void main(String args[])
{
S2 s = new S2();
s.test();
}
}


i think u know the output ..... here i m

how can i explain thisss .... plz help

thnaxxxx

sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#2: Mar 20 '07

re: plz explain this ?????


Well, how would you explain it? What do you know is happening (hint, you already told us with the output)?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Mar 20 '07

re: plz explain this ?????


Quote:

Originally Posted by dmjpro

how can i explain this .........

class S1
{
static void Static()
{
System.out.println("Here i m ...");
}
}

class S2 extends S1
{
void test()
{
Static();
}
}

class StaticTest1
{
public static void main(String args[])
{
S2 s = new S2();
s.test();
}
}

i think u know the output ..... here i m

how can i explain thisss .... plz help

thnaxxxx

Well, wouldn't you expect that output DJ? You did call the method called static which prints that message.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#4: Mar 20 '07

re: plz explain this ?????


actually what i understood ..... that is

the static method never in herited so how the class S2 knows Static

and how the static method resolved by compiler and what compiler tells the JVM to do???????

plz clearify me in details ..... thanxxx
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#5: Mar 20 '07

re: plz explain this ?????


Quote:

Originally Posted by dmjpro

actually what i understood ..... that is

the static method never in herited so how the class S2 knows Static

and how the static method resolved by compiler and what compiler tells the JVM to do???????

plz clearify me in details ..... thanxxx

What does the call to instantiate S2 do? (What does it call?)
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#6: Mar 20 '07

re: plz explain this ?????


JVM call these function but JVM take decision according to compiler ???

as i previously i knew from this site ...... that the staic method resolved during compile time ... but here i stuck

i think as the S2 extends S1 then inside test() method the Static() calls internally converted into S1.Static()....

m i right ??????

plz help .....
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#7: Mar 20 '07

re: plz explain this ?????


Quote:

Originally Posted by dmjpro

i think as the S2 extends S1 then inside test() method the Static() calls internally converted into S1.Static()....

The call to instantiate S2 creates the Static() method which is inherited through the 'extends' keyword, and then is invoked in the StaticTest1.

I'm not too sure on the internals of this - I do not believe that an S1 object is created separately from S2 (as S2 extends, it is considered a member of the superclass Object, and then S1, and then S2 - technically every S2 is an S1...). I'm sure another expert on here could verify/disprove this...
Reply