Object and instance are same things?


Object and instance are same things?



While reading about synchronization, I read static synchronized method locked on class object and synchronized method locks on current instance of an object.
I have confusion what is class object and instance of an object? Don't object and instance are same things?





The class is like a recipe, an object instance is like a cake. You need the recipe to make a cake. You can make more than one cake. You need to synchronize on the recipe though, because otherwise you might add eggs when you should be frosting. Once the cake is made, you need to synchronize on it unless only one person is eating it. And that ends my cake metaphor. Because now I want cake.
– Elliott Frisch
Jul 1 at 6:33




3 Answers
3



For every class loaded into JVM , an instance of class Class will be created.
for ex:
Student student = new Student()



when above line of code executed class (Student) get loaded into JVM first bcz JVM needs template to create object .And based on template JVM creates instance of Student in heap .Along with Student instance JVM creates instance of class Class in heap.This class instance all the information about STudent like number of constructors,methods,variables etc we use for reflection.



Now all the instances (object) created will have a lock associated with then which comes into picture when executing synchronized methods or blocks (which can be static or instance) . if you are executing any static methods in synchronized context then lock of Class class (of STudent) will be acquired until its completion .





Dear spa , thank you for the answer , could you please provide me the source of the answer so that I can read this in depth.
– Navneet Kumar
Jul 2 at 7:53





Hi navneet u go through ''inside the java 2 virtual machine" by bill venners
– spa
Jul 2 at 9:30



The operative word there is class. Every class has an instance of java.lang.Class that represents it in the current classloader.


java.lang.Class



Consider this simple example:


public class MyClass {
public synchronized void instanceMethod() {
// This method will synchronize on the instance of MyClass that called it
}

public static synchronized void staticMethod() {
// This method will synchronize on MyClass.class
}
}



I think whatever you are reading this from could have worded this in a better way.



The difference between synchronised instance methods and synchronised static methods is that the latter "locks" the class while the former locks the instance of that class on which the synchronised method is called. See this answer for more info.



Assuming the author knows what he/she is talking about, the word "class object" creates confusion, because classes themselves are not objects. Is he/she talking about instances of Class<T>? Probably not. The author should have just said "class".


Class<T>



As to what's the difference between "object" and "instance" outside of the context of synchronisation, they mean really the same thing - those things that you create by calling constructors (and string literals).



Personally, I think that the word "object" has a higher level abstraction. When I am talking at a low level of abstraction (talking about individual lines of code e.g. "I created an instance of Foo here by writing new Foo()"), I would use "instance". When I am talking about things on a higher level (like how the whole system works), I tend to use "object". But this might be just me. Other people might use them interchangeably.


Foo


new Foo()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API