Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the main goal of using .class field

 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I've seen a lot of code that uses .class field. I wanna know when I should use .class field and when .getClass() method.

Can somebody help me?

And one more question: Where can I read something about .class field?

Thanks.
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George,

For more information on your query, search for Reflection API in the Java documentation.

Regards,
JD
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the above-mentioned tutorial will answer this question.

To use getClass(), you have to have an instance of the class. To use the .class "field", you don't -- you can just use the name of the class. The implementation of the .class field adds a new static member variable to the class that invokes it -- i.e., if class A uses "B.class", then class A gets a new static member.

So my answer is that it's generally better to use getClass() when you can, and .class only when you need to -- i.e., when you don't have access to an instance of the class of interest.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The .class field and the getClass() method that's available to all classes returns the public static Class variable that's available to every object and that's instantiated when the class is first loaded.

Suppose we have a Class Foo, the class variable being public and static you can access it as Foo.class (this returns the Class object associated with Foo). Foo.getClass() also returns the same variable - so you could use either.

The Class object is the starting point for all reflection and here's an excellent sun resource that explains Reflection in detail.

Thanks,
Ram.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My quote ::: Foo.getClass() also returns the same variable - so you could use either.


Iam wrong - as the sheriff explains in the post above my previous, the getClass() method can be accessed only via an object of the class.

Thanks,
Ram.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of you.

Ok, then. Can you explain to me where in the javadoc for class Object I can read about class field?

I've find the description of how to use .class here,
but I've not find any JLS section about .class field.

In my mind tutorial is not a specification of any kind.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I've got your point, sheriff. But still, can you show me a section of JLS that describes .class?
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've checked it



Output:
class$test_class$Foo

So, I've made another try - added static field in class A


Output:
"A.java": the symbol class$test_class$Foo conflicts with a compiler-synthesized symbol in test_class.A at line 10, column 16

Ok. But still, which part of specification covers this "field"?
[ March 17, 2005: Message edited by: George Bolyuba ]
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've fond one interesting link relaited this topic.

Class replacement
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by George Bolyuba:
Ok. But still, which part of specification covers this "field"?


15.8.2
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

15.8.2


Thanks, Ernest.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic