Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Determine Reference Type in the Following Case

 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what is reference type of the Object created in Line 1...


Deepak
[ May 11, 2005: Message edited by: Mark Spritzler ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so first lets format your code so that we can read it by using the CODE tags that get created when you click the CODE button under the Add Reply button.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new q().printS1S2(); // 1


Nothing is created on this line, just a compile time error because you need to capitalize the "q".

Mark
[ May 11, 2005: Message edited by: Mark Spritzler ]
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ya after capitalising Q it will compile

here no reference type is declared so no reference type will be created
the answer will be

P.printS1
Q. prints2

the tricky point here is that ... thought instance variable is of Q
but static method of p will be called if the call is made from that class irrespective of instance variable...

hey other ranchers..is i m rt...?
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amit taneja:

the tricky point here is that ... thought instance variable is of Q
but static method of p will be called if the call is made from that class irrespective of instance variable...


What exactly are you saying ?

( that, printS1S2() will call P.printS1() and Q.printS2() ? that's correct )
[ May 11, 2005: Message edited by: soumya ravindranath ]
 
shetal bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain in more detail, why is the static method of P is getting called?
 
Kumar J
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static void printS2(){
System.out.println("p.PrintS1");
}
void printS2(){
System.out.println("P. printS2");
}


Can we have same method names printS2(non-static) and printS2(static) ?

[ May 12, 2005: Message edited by: Mr Kumar J ]
 
shetal bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think thats a typo error,
it is
_______________________________
static void printS1(){
System.out.println("p.PrintS1");
}
void printS2(){
System.out.println("P. printS2");
}
________________________________
else we will get a compilation error.
Deepu pls. confirm!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic