Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!

Peter MacMillan

Ranch Hand
+ Follow
since Jun 23, 2006
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 Peter MacMillan

In Canada and the USA, you can request an appropriate logo via this website. Have your certification information handy, select the appropriate logo, and complete the form.

Here are the specific ones:
Certified Programmer
Certified Developer
Enterprise Architecht

Certification FAQ
[ July 12, 2006: Message edited by: Peter MacMillan ]
if you synchronize the methods (or finer grained synchronization if you must) then only one thread can access the file at a time. So there is no problem with concurrant access because there isn't - it's done one at a time. As long as you honour record locks, you should be fine.

nb. this only works if you have something common to synchronize around. If you're using multiple instances of the data class, you might have to lock/synchronize on a shared instance of something else.

The record locking requirement ensures that one thread can lock, read, write, unlock without any other thread being able to update that record while it owns the lock. This doesn't mean that other threads can't read anything or update other records.
[ July 12, 2006: Message edited by: Peter MacMillan ]

Originally posted by J Brewer:
Can't you put static variables inside a static method?



No. Only the final modifier is allowed.
In other words, the start() method only starts the thread: it will not block (execution will continue).

You can block by calling the join() method, which will cause the current thread wait for that thread to finish.
Run this version of the program:



Notice which line gets run first (ie. which line is printing out the 0). You have, essentially, a race condition: The line at //2 will execute before the line at //1 - printing out the B:0.

The output is likely to be:

B:0
A:1
A:2
A:3

However, it could also be, although less likely:

A:1
B:1
A:2
A:3
I took mine (the 1.5 beta) on 3 days notice, without any studying, and scored 91%. So, um, to each his own . "Suitable timeframe"? It's an individual metric based on the time you have available to you, the willingness and ability to focus, and an aptiude for the material.

Originally posted by Shanel Jacob:
Hi, why is "protected" an illegal modifier then? I know Sun Java say so but I'd like to know the rationale behind it. Thank you.



You kind of answered this yourself in your original post.

There wouldn't be a point.

public: the class is available outside of the package
private: ?
package: the class is only availalbe within the package.
protected: ?

If you think about it, private, package and protected would all achieve the same goal - restricting access to the class to within the same package. Since packages can't be "inherited", there is no use for protected. And the default access modifier (package) does not allow external access, so private is made redundant.

I would gather that there are only two (public/package) in order to keep it as simple as possible.
"protected" is an illegal modifier for a class outside of an enclosing class.

ie:



The above code is illegal because protected is not a valid modifier for a non-enclosed class.



Here protected is legal and Foo will only be accessible to members in Bar or descendants of Bar (ie. Foo is a protected member of the Bar class).

see: Class Modifiers
You are correct in that i has not been initialized. However, the more important issue is the use of the static keyword.

Inside of an instance method, a static modifier makes no sense at all. Static variables must be fields of the class.
[ June 27, 2006: Message edited by: Peter MacMillan ]
As written, that code will compile and run cleanly without any output (4).

To understand why, examine the java API for the Thread.start and Thread.run methods.

As a hint, here is a modified version that will produce output:




hint: the default run method is essentially empty (well, it will try to use a delegate Runnable, but that's not applicable in this case). The code from your question overrides the start() method and calls the Thread.run() method.

Hope that helps.
[ June 27, 2006: Message edited by: Peter MacMillan ]
all that code and the answer is:


Well there are other problems, but that's the one central to your question.

plus is a method of the LongInt class. f is an instance of the LongInt class, so in order to call the plus method you write f.plus(...); which tells java to call the plus method of the object referred to by f.

Hope that helps a bit.
15 years ago

Originally posted by Supriya Nimakuri:
Can you please explain how the output is "AB,B"



The execute function will append the value of y to x (using the x.append method). The value of the a object will be "AB" and the value of the b object will be "B".

The y = x; line only affects the local y variable (which is a reference to the a object). The a variable is unaffected by this statement.


note at //nb under versions prior to jdk 1.5, you'd have to do:



I'm not aware of an API feature that can do this.
try:


and think why you need that second String.class
15 years ago
No, you will not get an exception. As it is written, there is no Dog object for the serializer to write (dog is uninitialized in Animal). The serializer certainly knows how to write null and uninitialized values to an output, so there will be no error.

The serialization error will occur if you initialize dog - ie. give it a value - because the Serializer will not know how to serialized the Dog class (it does not implement Serializable).

This example should help you with the behaviour:



Run that and you will get the output:

Got a dog, and his name is: Fido

Change the line maked by //1 to:



Run it and you will get two serialization errors (one for the read and one for the write).