Could instance also be called as state of a class?

qadeer37

Well-known member
Joined
May 1, 2010
Messages
63
Location
Hyderabad,pakistan
Programming Experience
1-3
when we create an instance of a class we also provide its state so are these both same and if not same what's the difference?
And also what's the difference when we use equals method for instance or for state?
 
The "state" you talk about is not the state of the class, but the state of the instance. Don't think of a class as a physical thing. A class is just a description of what an instance of that class has (properties) and does (methods). Think of what a person is. You know in your mind what makes a person, what a person can do, etc, but that knowledge in your mind cannot walk and talk. It's just an abstract concept that describes a person. It's not an actual person. That's what a class is. It's just an abstract description. Each person you know is an instantiation of that idea in your head. Each person you know is an instance of the Person class.

I'm not 100% sure what you're last question means but I'll assume that you're asking about the difference between reference equality and value equality. If so then, sticking with the person analogy, consider identical twins. They are an example of value equality. Everything about them is the same, so they are equal in that sense, but they are still two distinct people. On the other hand, if I refer to my mother and my father's wife then I am making two different references to the same person.
 
classes are reference types, so yes equality of classes are "referential equality"

structures are value types, so when you check an equality of two structures it compares their values. This is why two different 5's will be equal.
 
Back
Top