Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

findAncestorWithClass

 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HFSJ book on page 544 gives an example of findAncestorWithClass usage : findAncestorWithClass(this, WayOuterTag.class)

Well, I think I've got it pretty well how it works, but there's a big doubt on this: How are they using a this reference as an argument to a static method?.....I mean, does it not violate normal Java rules?...I'm confused...Somebody please throw some light on this...
[ December 06, 2006: Message edited by: Sayak Banerjee ]
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, in order to use a method that is within a class, that class must be instantiated.
A static method is the exception.

So based on that knowledge there is no problem passing arguments such as "this" to that method.
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's ok...but you cannot use a this reference in a static context....that's where my question lies....how is it possible here?
[ December 06, 2006: Message edited by: Sayak Banerjee ]
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you talk about "static context" ?
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything inside a static method or even the argument list of a static method is a static context....if I pass a "this" reference to findAncestorWithClass I'm actually using "this" in a static context....you can try this with a normal java program....try passing "this" in any static method and you'll get a compile time error...so why the deviation from this rule here?
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" refers to the current instance of the class where findAncestorWithClass(...) is called.
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Sayak, your point is correct.
Its really interesting.I am also waiting for the replies.
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the most amazing thing is...try passing "this" to findAncestorWithClass() and it works....WOAH....this is the first time I'm coming across such a situation...where you pass the "this" reference to a static method...

[ December 06, 2006: Message edited by: Sayak Banerjee ]
[ December 06, 2006: Message edited by: Sayak Banerjee ]
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SORRY GUYZ....i figured it out....it's something similar to this code snippet below...maybe I need to get those cows back in the pen again ....I think if I get confused at this rate I'll get de-certified...



[ December 06, 2006: Message edited by: Sayak Banerjee ]
[ December 06, 2006: Message edited by: Sayak Banerjee ]
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:roll: :roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic