wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes findAncestorWithClass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "findAncestorWithClass" Watch "findAncestorWithClass" New topic
Author

findAncestorWithClass

Sayak Banerjee
Ranch Hand

Joined: Nov 28, 2006
Posts: 292
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 ]

Turn on, tune in, drop out.
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

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.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Sayak Banerjee
Ranch Hand

Joined: Nov 28, 2006
Posts: 292
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

Joined: Jun 28, 2003
Posts: 547

why do you talk about "static context" ?
Sayak Banerjee
Ranch Hand

Joined: Nov 28, 2006
Posts: 292
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

Joined: Jun 28, 2003
Posts: 547

"this" refers to the current instance of the class where findAncestorWithClass(...) is called.
Sreeraj G Harilal
Ranch Hand

Joined: Apr 19, 2006
Posts: 310
Yes Sayak, your point is correct.
Its really interesting.I am also waiting for the replies.


SCJP 5.0<br />SCWCD 1.4<br />Preparing for <b>SCEA</b>.<br /><b>"I prefer an interesting vice to a virtue that bores."</b>
Sayak Banerjee
Ranch Hand

Joined: Nov 28, 2006
Posts: 292
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

Joined: Nov 28, 2006
Posts: 292
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

Joined: Apr 19, 2006
Posts: 310
:roll: :roll:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: findAncestorWithClass
 
Similar Threads
findAncestorWithClass() Versus getAdaptee()
findAncestorWithClass() Problem
getAncesterWithClass exists or not
How to get a SimpleTag Parent inside Classic nested Tag ( have a look)
Another Simple Tag & Classic Tag handler mock exam question - what's YOUR answer?