• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

class - Class attribute of an Java Object

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest,

Why is not implemented as a real field? Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic