Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Graphic class

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can we use Graphich class's method without instantiating it.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without an instance, you can only call static methods. To use instance (non-static) methods, you need an instance.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic