Tom Cockerline

Greenhorn
+ Follow
since Dec 20, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tom Cockerline

Hi Rob,
Here are my annotations:
12. X3 x2 = new X3();
Line 12 creates a reference variable named x2 of type X3. Then it creates a new object of type X3. (To help keep track of the objects that get created in this code snippet, let's call this new object "ObjectOne".) Finally, it references ObjectOne with the reference variable x2. We'll keep track of reference variables and the objects they refer to with this unofficial shorthand:
x2 --> ObjectOne

13. X3 x3 = new X3();
Line 13 is a lot like line 12. It creates a reference variable named x3 of type X3. Then it creates a new object of type X3. (Let's call this new object "ObjectTwo".) Finally, it references ObjectTwo with the reference variable x3.
x2 --> ObjectOne
x3 --> ObjectTwo

14. X3 x5 = x3;
Line 14 creates a reference variable named x5 of type X3 and gives it the same object reference that x3 has. What's that? Well, up in line 13 x3 was told to refer to ObjectTwo, so that's what x5 gets too.
x2 --> ObjectOne
x3 --> ObjectTwo
x5 --> ObjectTwo

15. x3 = x2;
Now x3, which was told to reference ObjectTwo up in line 13, is told to change its reference to whatever x2 is referencing. That would be ObjectOne, from line 12.
x2 --> ObjectOne
x3 --> ObjectOne
x5 --> ObjectTwo

16. X3 x4 = x3;
A new reference variable named x4 of type X3 is created, and told to reference whatever x3 is referencing.
x2 --> ObjectOne
x3 --> ObjectOne
x5 --> ObjectTwo
x4 --> ObjectOne

17. x2 = null;
Change x2 so it doesn't reference anything. Notice that the only reference variable referring to ObjectTwo is x5, so if x5 is changed, ObjectTwo will have no live references and will thus be eligible for garbage collection.
x2 --> null
x3 --> ObjectOne
x5 --> ObjectTwo
x4 --> ObjectOne

18. // insert code
Q: What two lines of code, inserted independently at line 18, will make an object eligible for garbage collection? (Choose two.)
In other words, we've created two objects. What can we do so that one of them will be left with no live reachable reference variable?
A. x3 = null;
x2 --> null
x3 --> null
x5 --> ObjectTwo
x4 --> ObjectOne
Since x4 is still referencing ObjectOne, this doesn't do it.

B. x4 = null;
x2 --> null
x3 --> ObjectOne
x5 --> ObjectTwo
x4 --> null
Since x3 is still referencing ObjectOne, this doesn't do it.

C. x5 = null;
x2 --> null
x3 --> ObjectOne
x5 --> null
x4 --> ObjectOne
This leaves ObjectTwo without any live references. Eureka!

D. x3 = x4;
x2 --> null
x3 --> ObjectOne
x5 --> ObjectTwo
x4 --> ObjectOne
This doesn't really change anything, since x3 and x4 were already referencing the same object, ObjectOne.

E. x5 = x4;
x2 --> null
x3 --> ObjectOne
x5 --> ObjectOne
x4 --> ObjectOne
This leaves ObjectTwo without any live references, since x5 has been changed to reference ObjectOne. Double eureka!!

Hope that helps.
Tom Cockerline
Hi Chandra,
This is an example of the JVM determining which method to use based on the type of the object on which the method is called, rather than based on the type of the object's reference variable.
In Java, a reference variable can refer to an object of its own type or to any subclass of its own type. This is known as polymorphism. The sup reference used in the call to the printVal method,

was declared as type Super, but it refers to a Sub object:

Under these circumstances, the JVM will run the method belonging to the class of the actual object (Sub) rather than the method belonging to the class of reference variable (Super).
This important topic is dealt with at greater length and with much greater clarity by Sierra & Bates in Chapter 7 of their book "Head Start Java". I heartily recommend it for your further edification.
Tom Cockerline
20 years ago
Howdy,
I have a JTextArea component that is displayed via a JScrollPane. Sometimes the text message that I place into the JTextArea component is too big to fit in the visible area of the JScrollPane, so the vertical scrollbar is activated. The first time my app encounters such a text message it positions it within the pane such that the top of the message is visible. So far, so good. However, subsequent messages are positioned such that the bottom of the message is visible, so that one has to scroll up to begin reading the message.
Is there a way to force the pane to always position itself at the beginning of the text area?
Thanks in advance,
Tom
20 years ago
Howdy,
I have a JTextArea component that is displayed via a JScrollPane. Sometimes the text message that I place into the JTextArea component is too big to fit in the visible area of the JScrollPane, so the vertical scrollbar is activated. The first time my app encounters such a text message it positions it within the pane such that the top of the message is visible. So far, so good. However, subsequent messages are positioned such that the bottom of the message is visible, so that one has to scroll up to begin reading the message.
Is there a way to force the pane to always position itself at the beginning of the text area?
Thanks in advance,
Tom
20 years ago
Yes, it works.
Now, I can double-click on one of my .qiz files and when my QuizCardReader starts it loads the .qiz file.
20 years ago
It turns out Windows Explorer passes the filename as the first argument to any apps it launches through extension association.
I modified the batch file to this:
java csci/quizcard/QuizCardReader %1
Now, the batch file passes along any arguments it receives from its caller (Windows Explorer in this case) to my java class. After modifying my class to conditionally handle the arg, it does what I was looking for.
Tom Cockerline
20 years ago
How can I double-click on a file in Windows Explorer to launch a java app and have the java app read the file that I double-clicked?
I have created a batch file that looks like this:
java QuizCardReader
This java class is actually an enhanced version of the QuizCard app from Head First Java (Sierra and Bates). It reads input files containing question/answer pairs, and I have created some of these input files and given them an extension of ".qiz". I have also associated that extension with the batch file above, so when I double-click one of my ".qiz" files from Windows Explorer it runs the batch file and launches the QuizCard app.
What I would like is for the QuizCardReader class to open and load the ".qiz" file that I double-click. If there were a way to pass the path and filename as an arg to the class, that would do the trick.
Thanks in advance,
Tom
20 years ago