• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Graphic class

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How can we use Graphich class's method without instantiating it.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without an instance, you can only call static methods. To use instance (non-static) methods, you need an instance.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic