aspose file tools*
The moose likes Beginning Java and the fly likes What is the main goal of using .class field 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 » Java » Beginning Java
Bookmark "What is the main goal of using .class field" Watch "What is the main goal of using .class field" New topic
Author

What is the main goal of using .class field

Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
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.


SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
George,

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

Regards,
JD
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

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

Joined: Jan 24, 2005
Posts: 489


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

Joined: Feb 18, 2005
Posts: 162
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

Joined: Feb 18, 2005
Posts: 162
Ok. I've got your point, sheriff. But still, can you show me a section of JLS that describes .class?
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
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

Joined: Feb 18, 2005
Posts: 162
I've fond one interesting link relaited this topic.

Class replacement
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by George Bolyuba:
Ok. But still, which part of specification covers this "field"?


15.8.2
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Originally posted by Ernest Friedman-Hill:

15.8.2


Thanks, Ernest.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the main goal of using .class field