File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is an object an Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is an object an "instance" of a class?" Watch "Is an object an "instance" of a class?" New topic
Author

Is an object an "instance" of a class?

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
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

Joined: Jan 07, 2002
Posts: 2205
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.


Rob
SCJP 1.4
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Sep 23, 2000
Posts: 3313
    
    7
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.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 07, 2002
Posts: 2205
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is an object an "instance" of a class?