File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class definition

 
Deepak Chandrashekar Avanna
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers,

I had a question regarding the following assignment.

Class cls = Item.class;

Can anybody tell me where is class for Item defined as I have not defined the 'class' for my Item class. I checked JLS for Object but did not find it there.

Thanks,
Deepak
 
Allan Jacobs
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Deepak,

It is possible to do better than find the class file for you. Assuming that your .java file compiles then it should be possible to train you to find it yourself.

If your program compiles then post the (1) package statement at the top of the file that contains the Item.class usage, (2) the import statements at the top of the same file, (3) your javac command line (especially any -classpath argument). If your javac command line contains no -classpath argument then post (3') the CLASSPATH setting for your environment.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is taken directly from the documentation of the Class class.
public final class Class<T> extends Object implements Serializable, GenericDeclaration, Type, AnnotatedElement

Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. 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. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

...It is also possible to get the Class object for a named type (or for void) using a class literal
(JLS Section 15.8.2). For example:


System.out.println("The name of class Foo is: "+Foo.class.getName());


(Emphasis mine)
[ October 25, 2006: Message edited by: Garrett Rowe ]
 
Matt Russell
Ranch Hand
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a class literal:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#251530

It's automatically defined by the language, so you don't have to declare it yourself.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepak Chandrashekar Avanna:
... Can anybody tell me where is class for Item defined as I have not defined the 'class' for my Item class...

Welcome to JavaRanch!

I'm not entirely clear on what you're asking: Are you saying that you can't find a definition for Item.class? Or are you asking about the Class.class? It looks like both interpretations have been addressed above.
 
Deepak Chandrashekar Avanna
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Allan,

Thanks for the reply.

I am not clear about your post. To put my question in better way, I just wanted to know where is the "class" in Item.class defined. Is it inherited from Object?

Thanks,
Deepak
 
Deepak Chandrashekar Avanna
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody.

I got it. It is the literal.

Thanks,
Deepak
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepak Chandrashekar Avanna:
...I got it. It is the literal...



You might also be interested in this thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic