user1001832
user1001832

Reputation: 55

difference between static class and normal class

I am new to Java, and was reading synchronized blocks stuff. I got confused in one of the statement, that during the static class the synchronization uses class instance and normal class uses current object for locking.

Now when both the classes are same, the only difference is that one class is static and other one is normal. Does this make any changes to the interpretation.

Then again my next question will be that in how many ways we can achieve synchronization.

Thanks

Upvotes: 3

Views: 3095

Answers (5)

Shivan Dragon
Shivan Dragon

Reputation: 15219

You confuse the notions of Class, static, instance and how the synchronized block works.

A class is like a blueprint and an instance of that class is like a house build from the blueprint. You can have many houses that are built from the same blueprint just as you can have many instances of a class. A class can have instance methods (non-static, normal if you wish) and static methods. Just like a house (the implementation of a blueprint) can have a light switch function (method) which only makes sence when an actual house exist, in the same way non-static (instance) methods are usable only when you make an instance of a class. On the other hand, imagine that the house blueprints has a button which, upon pressing will calculate the area of the house. That's a function that can work directly on the blueprint but can just as well be used on a house, just like static methods can be used with a (non-instanciated) class but make sense to use in an instance of the class as well.

Synchronized methods when used aquire a lock on the thing that uses them. If you have a static method (either in a non-instantiated class or from an instance of the class) it will aquire the lock on the class, since the static methods is pertinent to the class (not the instance). if you call a dynamic method (which you can only do from an instance of the class) it will aquire a lock on the instance, not on the class.

Upvotes: 2

lpinto.eu
lpinto.eu

Reputation: 2127

When a method is declared synchronized, that means that only one call can be performed to that method at a time. When a class have more than one synchronized, that creates a synchronized API, this means that can happen only one call to all of this methods at a time.

A static method isn't an instance methods, it means that you don't call it from an object instance, but from the class it self.

When in a concurrent ambient, when there are various objects calling methods from a specific object instance this methods need to be synchronized, in order to ensure that only one occurs at a time.

Upvotes: 0

java_mouse
java_mouse

Reputation: 2109

When the static methods are synchronized, the locking is done on the "class" instance itself. That means, if you have a static synchronized method is executed, none of the other static synchronized method can be executed.

Upvotes: 0

ninjalj
ninjalj

Reputation: 43688

It is actually static methods vs non-static methods.

Static methods can be called without an object (i.e: no this), so they use the class' object's lock.

Non-static methods use the object's (this) lock.

Upvotes: 1

alf
alf

Reputation: 8513

It's not about classes — it's about methods.

synchronized methods are synchronized on the instance; static ones have no instance, so the synchronization is performed on the corresponding Class instance.

Upvotes: 3

Related Questions