File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interfaces and locks

 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you say in detail how "statics methods are synchronized on an object"?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Wilson
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic