*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized" Watch "synchronized" New topic
Author

synchronized

wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
48. From the following declarations select all correct variables/method
declarations?
Button bt = new Button ("Hello");
A) public transient int val;
B) public synchronized void Test() ;
C) bt.addActionListener( new ActionListener () );
D) synchronized ( this ) {
// Assume that "this" is an arbitrary object instance.
}
Answer 48:
A) public transient int val;
D) synchronized ( this ) {
// Assume that "this" is an arbitrary object instance.
}
Explanation:
To define transient variables just include "transient" keyword in the variable definition. The transient variables are not written out any where, this is the way when you do object serialization not to write the critical data to a disk or to a database.
The "synchronized" keyword controls the single resource not to access by multiple threads at the same time. The synchronized keyword can be applied to a method or to a block of code by passing the arbitrary object instance name as an argument.
--------------
why B,C are wrong???
Jim Bertorelli
Ranch Hand

Joined: Nov 28, 2001
Posts: 136
B is wrong because neither it has a method body nor it is declared to be abstract.
C is wrong because ActionListener is an interface. You can't just do new ActionListener.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Jim is right...
moreover, B would be correct if it was written like
public synchronized void Test(){} // note the semicolon has been removed and curly braces added
and C would be correct if it was written like
bt.addActionListener( new ActionListener (){
public void actionPerformed(ActionEvent e){
...
}
});
Note that when you create an anonymous inner class by implementing an interface, which is the case here with ActionListener, you have to implement all methods specified in the interface.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Being picky, D is not right because it is not a method declaration


SCJP2. Please Indent your code using UBB Code
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You are right Jose but in this case I think the author seemed to care about correct wording (since he/she mentions both variable and method) but forgot to mention block...
the question should be:
From the following declarations select all correct variables/method/block declarations?

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: synchronized
 
Similar Threads
someone please explain the answer
Why autoboxing doesnt work here???
JLS tips
Exam 9, Q24 Dan Chisholm
IBM Mock test