File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interfaces and locks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interfaces and locks" Watch "interfaces and locks" New topic
Author

interfaces and locks

Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
I understand that every class in Java is implicitly derived from class Object, thus all classes declared in java have a lock for synchronization.
I am wondering if interfaces like classes also derive from class Objcect and therefore have locks? It would sound reasonable to say no since interfaces are implemented classes that have locks!
Does anyone know for sure if interfaces have locks?


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
interfaces are just that- pure interface, no implementation. They are abstract. You can't instantiate them. They are not concrete objects. So they do not have locks.


Rob
SCJP 1.4
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Rob thanks, I wanted to get a 2nd opinion, seems like I came across a mock exam with a wrong answer...I'll see if I can dig up that question
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
When an interface is loaded a Class object is created for representing it. Thus it is possible to synchronize on that object:

This code compiles ok.
However I would say that only objects have locks. Thus no interface or class have locks, only instances of a class, arrays and the Class objects.
I am afraid this is another semantic issue.
I mean that if you want to consider a class having locks, you also should consider interfaces. well, or both not having.


SCJP2. Please Indent your code using UBB Code
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I don't think it's semantic at all. Like you said, only objects can have a lock. A Class object is still an object, and thus able to have a lock. But an *interface* itself, there's no way to lock on that. You're locking on the Class object representing the interface, not the interface.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I am sorry my previous post could be confusing.
Semantics issue: what something has a lock means?
a)properly speaking only objects has locks.
b)classes "has" locks but interfaces "hasn't". The use has imposed that we tell a class has a lock when synchronizing like this:
synchronized static void aMethod() {}
However it is not possible to sayt that an interface has a lock applying the same reasoning. Because interfaces methods will reject static or synchronized keywords.
c)classes really hasn't got locks. statics methods are synchronized on an object, not on the class itself.
Richard Wilson
Ranch Hand

Joined: Jan 12, 2002
Posts: 57
Can you say in detail how "statics methods are synchronized on an object"?


Richard Wilson
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
static methods are synchronized on an object representing the current class and whose "type" is Class.
Synchronized static methods in class MyClass are synchronized on MyClass.class (not the file, the "class" is a class literal here) which is an object of class Class which represent the class MyClass.
Moreover the reason why synchronized cannot be applied to interface methods is because synchronization is an implementation issue and interfaces do not have any implementation.


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

Joined: Jan 12, 2002
Posts: 57
Hi,can you share me with some examples on that?
Moreover,I still did not understand what is a "class literal" and the whole process class literal does on synchronized code.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Ok, say we have a class named MyClass. Now let's read those two JLS lines I gave you together:
A class literal is an expression consisting of the name of a class, interface, array, or primitive type...
MyClass
...followed by a `.' ...
MyClass.
...and the token class.
MyClass.class
That's it. This is a class literal.
The type of a class literal is Class.
that is (MyClass.class instanceof Class) yields true.
It evaluates to the Class object for the named type (or for void) as defined by the defining class loader of the class of the current instance.
MyClass.class is the same object you would get by invoking Class.forName("MyClass");
It is an object representing the class MyClass. With it you can retrieve everything that is declared inside class MyClass. Have a look at the java.lang.Class API docs and look what you can do with a Class object. From there on, it will seem clear to you what a Class object is for.
[ February 15, 2002: Message edited by: Valentin Crettaz ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: interfaces and locks