Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is an object an "instance" of a class?

 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JQ+ question was:
An object is:
and the answer was "an instance of a class"
This definitely was the best answer to choose from, but I'm just wondering: isn't a class with all static variable and methods still an object? There is no instance of this class ever created but isn't it still considered an object?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in the sense the question is asking. Every class does have a single Class object associated with it. But you don't have an actual instance of the class. For example, we use Math a lot for it's methods, but they're all static. There is no instance of Math that we use. Sometimes we just call this a "static class", meaning, we don't actually instantiate any instances, we just use static methods/fields.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob:
Every class does have a single Class object associated with it.]


I am not sure what you mean here. You mean "this"?
Which is actually a reference to the object on which the method is being invoked. Please enlighten me on this.
victor
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the method getClass() in Object. It returns a Class object, which represents the actual runtime class of the object. Note that this isn't covered on the SCJP2.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It returns the runtime class of an object. Not a
Class object. Right? It will basically return
the object of type Class that represents the runtime class of the object. Thus, we have one
associated Class, not object. Right?
victor
Originally posted by Rob Ross:
Look at the method getClass() in Object. It returns a Class object, which represents the actual runtime class of the object. Note that this isn't covered on the SCJP2.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by victor gu:
It returns the runtime class of an object. Not a
Class object. Right?

No, wrong.
getClass() returns an instance of a Class object.
Look at getClass() and the Class class.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited this message. I think my sample code is not very accurate. Based on API, arrays, primitive types, void represents Class objects. For example: Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.
That means other reference types are not included
since they have their own class type. Thus, they do not have an associated Class object.
This is also from API:

getClass()
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
Returns:
the object of type Class that represents the runtime class of the object.

[ February 09, 2002: Message edited by: victor gu ]
[ February 09, 2002: Message edited by: victor gu ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is toString() a static method? Can you call it on a class, or do you need an instance of a class on which to invoke it?
Think about it!
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an object, there MUST be a class from which it has been instantiated. So, it is safe to say that an Object is an instance of a class. (In fact, this is the definition of Object)
A class that has only static methods is still a class. It cannot be an object.
The java.lang.Class class that Rob is talking about is the class that represents the actual class of the object. For example, if you have:
BaseClass b = new SubClass();
java.lang.Class c = b.getClass(); -> this will return an object of class Class which represents SubClass ( and not BaseClass).
Math in itself is just like any other class. As Rob said, the JVM keeps a java.lang.Class "object" that represents this class in the runtime.
But again, that object is of class java.lang.Class not of class java.lang.Math.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmmm, this is pretty clear explanation. I confused class object with Class object. A Class
object is not an object, its a runtime class represented by this Class "object". Oh la la..
victor
Originally posted by Paul Anil:
If you have an object, there MUST be a class from which it has been instantiated. So, it is safe to say that an Object is an instance of a class. (In fact, this is the definition of Object)
A class that has only static methods is still a class. It cannot be an object.
The java.lang.Class class that Rob is talking about is the class that represents the actual class of the object. For example, if you have:
BaseClass b = new SubClass();
java.lang.Class c = b.getClass(); -> this will return an object of class Class which represents SubClass ( and not BaseClass).
Math in itself is just like any other class. As Rob said, the JVM keeps a java.lang.Class "object" that represents this class in the runtime.
But again, that object is of class java.lang.Class not of class java.lang.Math.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by victor gu:
hmmmm, this is pretty clear explanation. I confused class object with Class object. A Class
object is not an object, its a runtime class represented by this Class "object". Oh la la..
victor

What do you mean an Class object is not an object? There are no "special" objects in Java. An object instance of type Class is an object as much as a String instance or a Frame instance is an object!!
AND , we're loosing track of the original question.
It was a generic question about the definition of an object as "an instance of a class". This is a correct definition.
Note, we're NOT talking about the Object class with a big "O". Just a regular OO-style "object" entity. An object is an instance of a class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic