Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Boone 1.1 Mock # 34

 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One last ? on this mock
Question 34: The method setBackground() defined for the Graphics class
(...)
b) takes an instance of class color
(...)
f) changes the background color for the associated Component object
-------
these are the 2 right answers. First, I can't find a setBackground() method for the Graphics class, so presumably this means the setBackground() method of the Component class. But my question is, for b, is it accurate to say the method takes an "instance" of the Color class? Color.red (for ex.) doesn't look like anything is being instantiated to me, it looks like a static reference.
Thank you very much
Eric B.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the question is put, b is quite correct. When you invoke
setBackground(Color.red) for example, you indeed do not use any instance of the color class. However it is quite possible and legal to do something like this
Color myColor = new Color(255,255,255);
setBackground(myColor);
Here we are using an instance of the Color class that I created with the RGB values 255,255,255 (this is the color white, I guess).
So the method can use the color constants as well as instances of the color class.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric,
1.The method setBackground() defined for the Graphics class
Your doubt is correct. setBackground/setForeground comes from Component class. Graphics class does not have any setBackground/setForeground. Instead it has only one setColor(Color c) method .
2. takes an instance of class color
Here the author is correct. The reason is , the Color class has all static (class) variables Color.red/Color.blue/Color.green... etc are all in fact a reference to a Color object . In other words, read the above author's statement as takes an object (instance )of class color . In OOP, the 2 words instance and object are interchangly used for each other. Here the sentence 'instance of a Color' means an object of Color. Which means the Color.red/Color.green are static references which are refering to a PHYSICAL COLOR OBJECT in heap. Since these Color.red/oColor.green are static references the objects are created when the Color class is loaded itself.
I give to a simple example. See the foll Color class carefully. This is Sun's source code of Color class. I digged into the source code and extracted this class defn.
<pre>
public class Color implements Paint, java.io.Serializable {

public final static Color white = new Color(255, 255, 255);
public final static Color lightGray = new Color(192, 192, 192);
public final static Color gray = new Color(128, 128, 128);
public final static Color darkGray = new Color(64, 64, 64);
public final static Color black = new Color(0, 0, 0);

}

</pre>
I have cut down the source code. See above what's really happening. The static class vars
Color.black/Color.lightGray/Color.gray... are all static references to REAL PHISICAL OBJECTS(instances) in HEAP. Do you get the point now ?. an instance of a Color means an object of Color class. It is not related to instance/class members.
I read this info from somewhere from some article. Since in Java , we are always talking about objects, and there is also a SPECIAL ansestor class Object, how can we pronounce these 2 words differently while talking from our mouth. . How can we diferentiate if you are talking the physical objects and the Class Object. While writing this is ok. We can put CAPITAL O for class Object and LOWERCASE 'o' for objects. But during conversation it is really difficult to distinguish these 2 words. So the Object orientad lang people invented one more word instance for referring to 'object' (real physical objects created from a class).
So I think this story will help you what's actually happening here and the justification for your doubt.
regds
maha anna

[This message has been edited by maha anna (edited May 07, 2000).]
 
Eric Barnhill
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your story helps a lot. I did not think of the word "instance" that way before, I just saw it and thought "no static!". Now the question makes sense.
Thanks to both of you
Eric
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic