• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class definition

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody.

I got it. It is the literal.

Thanks,
Deepak
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic