Forums Register Login

Class definition

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Thanks everybody.

I got it. It is the literal.

Thanks,
Deepak
+Pie Number of slices to send: Send
 

Originally posted by Deepak Chandrashekar Avanna:
...I got it. It is the literal...




You might also be interested in this thread.
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 703 times.
Similar Threads
Recursive program help
EJB3 Entity Relationship
Class definition
HELP NEEDED~~
warning: [unchecked] unchecked method invocation
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:07:57.