aspose file tools*
The moose likes Beginning Java and the fly likes Graphic class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Graphic class" Watch "Graphic class" New topic
Author

Graphic class

memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hi,
How can we use Graphich class's method without instantiating it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Without an instance, you can only call static methods. To use instance (non-static) methods, you need an instance.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

I suspect we're talking about java.awt.Graphics. You personally don't instantiate it; you implement paint(), or paintComponent(), and then Java calls that method for you, passing an instance in to your method.


[Jess in Action][AskingGoodQuestions]
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
For example, we can use nonstatic drawRect method of Graphic class without instantiating its object. So how can it be ?
Why do not we instantiate its object ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

You don't instantiate it, but it exists just the same; the Java libraries create it and pass it to you as a method argument. There are many objects that you use, but don't create yourself: for example, System.out (a PrintStream) and System.in (an InputStream).
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think you need to step back and ask who calls the paintComponent() method. (At least I assume this is the context you are using.) If you have done things correctly, you never call this method explicitly yourself. Instead, the operating system sends a message and a Java class from the API calls it for you. However, before Java calls the method it HAS to create (instantiate) a Graphic object somehow. This is what allows you to call non-static methods. An object IS instantiated, even if it doesn't appear explicitly in your own code.

If you need more help with understanding how paintComponent() works at a geeneral level, you should google for information on "callback functions".

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Graphic class