aspose file tools*
The moose likes Cattle Drive and the fly likes ghost method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "ghost method" Watch "ghost method" New topic
Author

ghost method

juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
I finally managed to get SortNames running
using an interface and its obligatory
method; I am wondering though, why the program
runs without me actively using (calling) this method;
it seems to be called automatically by implementing the interface??
Is there some rule or general explanation to this?
thanks for un-ghosting this,
Juliane
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Maybe you have to look at whatever is doing the sorting for you? What are its formal parameters as listed in the API?
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by juliane gross:
I am wondering though, why the program
runs without me actively using (calling) this method; it seems to be called automatically by implementing the interface??

It's being called way, way, way behind the scenes by code that you've set in motion by calling a particular method on the Collections class.
(I think I sketched this process out once in an old post, but I can't seem to find it now.)
The fact that you have implemented the interface and passed the implementing object along to the process that needs it is what makes all this work.
Think for a second how powerful this is in this case. You haven't had to worry about coding complicated sorting algorithms or data structures. All you've had to do essentially is define a rule for the process to follow, then set it all in motion. Pretty cool, no?
Is there some rule or general explanation to this? thanks for un-ghosting this,
This might not exactly be the kind of rule or explanation you're looking for, but I think it might help:
Any time you're told in Java that in order to do something you want to do you should either override some particular method or implement some particular interface (which amounts to writing some method(s)), part of the trick is realizing that something somewhere will be calling that method. (Sometimes you may want to know the details of what's calling the particular method. Sometimes you may not care.)
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
If you *really* want to see where your method is getting called, take a look at lines 1237, 1249, and 1256 in the source for the java.util.Arrays class.
But don't say I didn't warn you!
The beauty of the exercise is that you don't have to know what's going on at these lines of code in order to use them to do want you want.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Barry is, of course, excused from any reading of source code, since we know already that he doesn't like to.
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
thanks, Michael!
I will check out that source code ..
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I did my share of reading source code when debugging VMS Operating System device drivers! On microfiche and assembly language, or Bliss (Whatever happend to Bliss?)
AND, as Marilyn and Pauline will probably tell you, I only write source code now, I never read it
[ January 20, 2003: Message edited by: Barry Gaunt ]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
If you go Java source code surfing, Juliane, it'll probably be easier to follow if you start from the methods you're using in the Collections class, rather than from the lines in Arrays that I pointed out.
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
Michael wrote:
If you *really* want to see where your method is getting called, take a look at lines 1237, 1249, and 1256 in the source for the java.util.Arrays class

I couldn't find a link to the source code at API, or Sun's homepage.
Where exactly can I read that source code?
It should be somewhere in the internet, or do
I have to get a book??
I am embarrassed by my ignorance, but hell, I never pretended NOT to be a Java greenhorn..
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You should have a file like src.zip somewhere in the load of stuff you extracted to get the j2sdk1.4.1 kit.
By the way it's got a big label on saying "Enter at your own risk!"
-Barry
[ January 24, 2003: Message edited by: Barry Gaunt ]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
I think there's some disclaimer from Sun that they don't guarantee that the source code provided is what they actually compile into Java, but that it's some "typical" implementation.
Isn't it wild to see how much Java is written in Java itself.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
You can unzip (or unjar) src.jar which is usually in the base (home) directory of your sdk (something similar to C:\jdk1.3.1 or C:\j2sdk1.4.1)


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Michael Matola:
I think there's some disclaimer from Sun that they don't guarantee that the source code provided is what they actually compile into Java, but that it's some "typical" implementation.

I've heard others say that, but I've never seen any disclaimer like that.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by juliane gross:

Is there some rule or general explanation to this?

You might want to do a google search on the "Strategy Design Pattern".


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
wood burning stoves
 
subject: ghost method