• Post Reply Bookmark Topic Watch Topic
  • New Topic

class - Class attribute of an Java Object  RSS feed

 
mini mehta
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Does anybody knows when the "class" - an Class attribute was introduced in "Object" class? Even though "class" attribute is part of the "Object" class, it is not part of javadoc, is there any reason, why?


Mini
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mini mehta:

..... it is not part of javadoc, is there any reason, why?


Mini


The class object has no Class attributes and it never has had.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see it as a field implictly available with all java objects.
 
mini mehta
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nigel Browne:


The class object has no Class attributes and it never has had.


What about String.class or StringBuffer.class?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not in the Javadoc because it's not "real". It's not a member variable of Object (or any other class) -- it's an abstraction implemented by the compiler. If you use "X.class" in a program, then somewhere in your code, either in class X or, if X is a precompiled class, in the caller's class, the compiler will create code to call getClass() on an instance of X, or call Class.forName("X"), and cache the result in a private static member.

As to when it was added: I believe it was a JDK 1.2 feature, but I could be wrong; it could have been 1.1.
[ November 16, 2004: Message edited by: Ernest Friedman-Hill ]
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

Why is not implemented as a real field? Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:
Ernest,

Why is not implemented as a real field?


Well, it could be, I suppose, but at the time they were adding it, old class files wouldn't have it, and new ones would, and so obviously that would be a mess. It was presumably easier to do it this way. Is there something you don't like about the way it's done?

This reminds me of the "length" pseudo-member in arrays, which I personally detest -- why wasn't it defined to be a method instead?
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I don't like is - class is not a member field at all but we use it as a member of all Objects. Also, why do we need this field when getClass() is there?

From JLS
The members of an array type are all of the following:


The public final field length, which contains the number of components of the array (length may be positive or zero)
The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions
All the members inherited from class Object; the only method of Object that is not inherited is its clone method
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!