If you click on this stuff and give your money to some city slickers, JavaRanch gets a kickback. Hoooooooo Doggy!     
Do not post questions from the actual certification exams. If you see a question from an actual exam, please e-mail the moderators privately to let us know. To discuss a question from a mock exam, please mention which mock exam and the question number (if provided) so we know it's not from the real exam.



UBBFriend: Email This Page to Someone!
  JavaRanch Big Moose Saloon
  Programmer Certification
  Boone 1.1 Mock # 34

Post New Topic  Post A Reply
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   Boone 1.1 Mock # 34
Eric Barnhill
ranch hand
posted May 06, 2000 12:20 AM     Click Here to See the Profile for Eric Barnhill     Edit/Delete Message Reply w/Quote
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.

IP: Logged

Herbert
unregistered
posted May 06, 2000 09:12 AM           Edit/Delete Message Reply w/Quote
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.

IP: Logged

maha anna
bartender
posted May 07, 2000 02:31 PM     Click Here to See the Profile for maha anna   Click Here to Email maha anna     Edit/Delete Message Reply w/Quote
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.


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);

}

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).]

IP: Logged

Eric Barnhill
ranch hand
posted May 07, 2000 10:17 PM     Click Here to See the Profile for Eric Barnhill     Edit/Delete Message Reply w/Quote
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

IP: Logged

All times are MT (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | JavaRanch

Powered by: Ultimate Bulletin Board, Version 5.44a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 2000.