David Ng

Greenhorn
+ Follow
since May 11, 2004
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 David Ng

Given A a = new B();

If you want to access the s1 in B, you need to cast it, i.e.

(B(a)).s1

You can also add a getStr method in both class A and B:

String getStr() { return s1; }

In class C, instead of using a.s1 and (B(a)).s1, you can just say:

a.getStr()
19 years ago
I think the answer should be D becuase in line 15 it pass the reference to object B to a method s. If inside method s it pass the reference to a thread it will make object B ineligible for GC until the thread terminates. However, without seeing the code of method s, there is no way to determine what happens to the reference B.
Your code has two problems. The first one is the use of the run instead of the start method as pointed out by Corey. The second problem is you don't have a Thread object in your main mehtod. If you just change ntX.run() to ntX.start() you will get a compilation error.

To make the code work, you need to either:

1. Change the declaration of NiceThreads to "public class NiceThreads extends Thread" and start the threads in your main method with nt1.start(), nt2.start() ... etc.

2. Keep the NiceThread declaration as is. Change the main method code to:



Hope this helps.
It would help if you post some detail about the PaymentObject.
19 years ago
I don't believe the type of exception matters. If a return is executed within a finally block, it will nullify all exception (including pending one). After control is retuned to the calling method, execution will continue as normal.

The program below will run to normal completion and prints "Normal termination".

However, if line 20 is removed or commented out, it will terminate with the following:

My Exception
at Q0001Exception.method(Q0001Exception.java:15)
at Q0001Exception.main(Q0001Exception.java:7)
Exception in thread "main"

In the following code, how many objects will be created?

String x = "abc";
String y = new("abc");
String z = new("abc");

Could someone explain this case?
The M&R book has more detail and is more difficult to read. If you are new to Java, I will say start with the K&B book first.
The reference itself is passed just like a primitive. You can modifies the field refered to by the reference but any changes made to the reference stays local.
Barry, You have an excellent point about the synchronization object being in the string literal pool.
I got curious and wrote a test program to try it out. And you are 100% correct! All the threads are synchronizing on the same object.
That is an excellent catch!
Could some one tell me if the following is true?
1. An annoymous class MUST either extend another class or implement an interface?
2. Any new (i.e. not defined in the superclass or interface) method is not acessable. If this statement is false, how would one access it?
Thanks
At the end of the loop, q1 holds the reference to the last object allocated thus making it ineligible for GC.