aspose file tools*
The moose likes Beginning Java and the fly likes method calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method calls" Watch "method calls" New topic
Author

method calls

Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
I am wondering if EVERY method defined in Java is automatically called at least once??? Like in C++ you can define a method, but if you don't call the method then it just isn't called.


if you don't know, then ask. if you do know, then share. love is knowledge.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Methods defined most certainly do NOT all have to be used.
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
the reason i asked this is because i copied an example out of my Deitel & Deitel book, that example uses this method that is defined but not called:
public void paint()
still, the compiler i guess still calls this method, that's what Deitel's overview of the simple program says. why is this? is there a list of classes and functions that all Java programs automatically import or call?
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
paint() is a GUI (Graphical User Interface) function. GUI's aren't like non-gui programs. There isn't a set flow of functions -- lots of different things could happen during runtime (mouse moving, minimizing and maximizing, typing, moving the window, etc.). The program that you write can't be expected to keep track of all this. That is why there is extensive GUI support in Java. Since I don't know if you're using Swing or AWT, I'll call the GUI library the Java GUI system.
It would take a lot of processing power to redraw the screen continuously. This may be necessary if you are making an animation. If you are just making a straitforward user interface, it's safe to say that nothing will need to be redrawn until some sort of event happens. This event could be that the window is covered by another window and exposed again, or that something is clicked on. The Java GUI system says, "Okay, I have no garentee that everthing looks right anymore, so I'll call paint()." It also call paint when the applet/application is first shown. Do I make any sense?
On the other hand your program might change something on the GUI itself that the Java GUI system won't be aware of. In that case, you must call paint() (or repaint()) to ensure that the user sees what the program thinks he/she sees.
Rowan Brownlee
Ranch Hand

Joined: Aug 07, 2000
Posts: 97
Hi Thomas,
Can't help on the question of "is there a list of classes and functions that all Java programs automatically import or call?"
As for the use of paint() and why the method is defined if the coding doesn't explicitly call it....
My understanding is (and correct me if I'm wrong) that any object that appears on the screen will have a paint method that draws the object on the screen. Sometimes you'll need to define a paint method. Other times this won't be necessary. Following are a couple of examples which might illustrate this idea.
Eg. of not needing to define a paint method
~~~~~~
If you want a button to appear on the screen, you don't need to define a paint method, as button inherits it's paint method from Component.
Extract from the API Component documentation.
~~~~~~~~~~~~~~~
public void paint(Graphics g)
Paints this component.
This method is called when the contents of the component should be painted in response to the component first being shown or damage needing repair... "
~~~~~~~~~~~~~~
When you make an AWT frame containing the button visible via setVisible(true), one of the things that usually happens is an invocation of repaint(), a method which calls paint(). You don't have to call paint() or even define a paint() method in this case - it's done for you.

Eg. when you'd need to define a paint method
~~~~~~~~~~
You might create a simple AWT frame and wish to display a line of text directly onto the frame (not on the title bar, but within the border of the frame itself), without using a label component. In this case, you could define a paint method and use the Graphics drawString method to display the text at a set position within the frame.
eg.
public void paint(Graphics g){
g.drawString("Text to display", 50,75);
}

Here, you need to define a paint method just to get your text to display. In this case, if you'd instead used a label component, you wouldn't need to define a paint() method, as label (like button) inherits a paint method from Component.

I'm wondering if the example of paint() in the Deitel book does something that's appears programmer defined (eg. like drawing a string, or filling a shape with colour) and so requires explicit definition of a paint method??
cheerio
rowan
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
If you look at the API for the Applet class you will see several methods that are called by the browser.

Sort of like the JVM's call to public static void main( String[] args ), the browser calls:

void init()
Called by the browser or applet viewer to inform this applet that it has been loaded into the system.

void start()
Called by the browser or applet viewer to inform this applet that it should start its execution

void stop()
Called by the browser or applet viewer to inform this applet that it should stop its execution.

void destroy()
Called by the browser or applet viewer to inform this applet that it is being reclaimed and that it should destroy any resources that it has allocated.

void paint(Graphics g)
Paints the container.
is inherited from the Container class. This method is called by the browser or appletviewer after the init method completes execution and the start method has started executing to draw on the applet. It is also called autometically every time the applet needs to be repainted.

"the compiler i guess still calls this method"
The compiler does not call methods. The browser or appletviewer calls these methods.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method calls