The Artima Developer Community
Sponsored Link

Java Answers Forum
the Class class

4 replies on 1 page. Most recent reply: May 15, 2002 11:29 PM by Bart Pelsmaekers

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Roshan

Posts: 3
Nickname: jrosh
Registered: May, 2002

the Class class Posted: May 3, 2002 11:58 AM
Reply to this message Reply
Advertisement
hi,

What is the use of class 'Class'. Is there any relationship with the 'Class' objects and the class file which results in compiling a java source file.


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: the Class class Posted: May 3, 2002 3:24 PM
Reply to this message Reply
Once a "class" has been touched there is a reference to it that will be kept until the VM ends !

On an important App it may therefore be important to straight away have a reference to all the classes you will use int he pgrm ?

Heres is littel snipplet that may allow you to do that straight away !
/** 
  * Tx to carlo van de sompel 
  */
public class LoadAllClasses
  implements Runnable
{
  Category log = Category.getInstance (LoadAllClasses.class);
  // Put preferable the complete class-name
  // package.sub_package.ClassName
  public static String ListOfClasses
    = {"Class1", "Class2"};
  public void run ()
  {
    for (int i = 0; i < ListOfClasses.length; i++)
    {
      try
      {
         Class.forName (ListOfClasses[i]);
      } catch (Exception e)
      {
         log.warn ("Exception while ");
      }    // End of try-catch
    }    // End of for-loop
  }   // End of run
} // End of Class


Please note, this is untested (straight out of the mold, as we say in French :-) )

Basically this will nicely help your code to start up slightly faster as the VM will not need to look up for the class has it is loaded !

Thomas SMETS,
SCJP2 - Brussels

Roshan

Posts: 3
Nickname: jrosh
Registered: May, 2002

Re: the Class class Posted: May 4, 2002 11:11 AM
Reply to this message Reply
hi again,

Thank you for your reply. But I am still confused with CLASS.
The following section is extraced from your book "Thinking in Java " ,Chapter 12 ,Run-Time Type Identification.

" There?s a Class object for each class that is part of your program. That is, each time you write and compile a new class, a single Class object is also created (and stored, appropriately enough, in an identically named .class file). At run-time, when you want to make an object of that class, the Java Virtual Machine (JVM) that?s executing your program first checks to see if the Class object for that type is loaded. If not, the JVM loads it by finding the .class file with that name. "

According to this the .class files that are produced by compiling the .java files are Objects of Type 'Class ', and that each time the JVM loads a class it loads this Class object.

I thought that the .class file represents a set of instructions (together with data) to the JVM. It is like an .exe file in windows, and what the JVM does is load the .class file and execute the instrucitons (byte code instrucitons) appear in the .class file.

And my understanding of the 'Class' class was it is just there to be used by anyone who needs to play with a objects at runtime.

I would be pleased if you could get me out of this confusion. Thank you !

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: the Class class Posted: May 5, 2002 5:45 AM
Reply to this message Reply
class MyClass{

}

declares a class named MyClass

the word class is all lower case.

the package java.lang has a class named Class.

Instances of the class Class represent classes and interfaces in a running Java application.

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.

There are explanations in the JavaLanguage Specification at:
http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html

Chapter 8 discusses Class at:

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#228205

Bart Pelsmaekers

Posts: 12
Nickname: ikken
Registered: May, 2002

Re: the Class class Posted: May 15, 2002 11:29 PM
Reply to this message Reply
for each object you make, the java virtual machine will create a Class object of it, wich gives you all the information about the object. With the class object, you will be able to find out al the methods, fields of the object.

This is called "innerspection"

Flat View: This topic has 4 replies on 1 page
Topic: Changing the pressed jbutton color? Previous Topic   Next Topic Topic: Links on Applet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use