5 Replies Latest reply: Nov 7, 2007 10:34 PM by 807603 RSS

    Class.foName() and classname.class

    807603
      plz tell me, difference between

      AnyClass{}

      Class.forName("AnyClass")
      and
      AnyClass.class
        • 1. Re: Class.foName() and classname.class
          baftos
          altafjava wrote:
          plz tell me, difference between

          AnyClass{}

          Class.forName("AnyClass")
          and
          AnyClass.class
          System.out.println( AnyClass.class == Class.forName("AnyClass"));
          will print true, assuming AnyClass is the fully qualified class name.
          The difference is in usage:
          String className = ...;
          Class clazz = Class.forName( className ); // The second form is not usable
          The second form is better when the class name is known at compile time.
          The first form would work, but if you make a spelling mistake the compiler will not tell you.
          • 2. Re: Class.foName() and classname.class
            807603
            ... also, forName may throw the checked exception ClassNotFoundException
            • 3. Re: Class.foName() and classname.class
              807603
              AnyClass{} is a class declaration in java. This is how you declare a valid java file with .java as extension.

              Class.forName("Anyclass"); forName method is used to dynamically load a fully qualified class in java. So the fully qualified name that you pass within quotes will be loaded by the JVM.

              AnyClass.class, is the form of a java class after compilation. Once you compile a AnyClass.java there will be an AnyClass.class created after successful compilation. This class file is nothing but byte code generated by the java compiler.

              I hope this helps you.

              --
              Choicest Regards
              Amit Singh Rathore
              www.proamit.com
              • 4. Re: Class.foName() and classname.class
                jschellSomeoneStoleMyAlias
                AnyClass.class, is the form of a java class after compilation. Once you compile a AnyClass.java there will be an AnyClass.class created after successful compilation. This class file is nothing but byte code generated by the java compiler.
                Interesting interpretation.

                I believe the OP was referring to the 'class' literal which is part of the java language rather than the output of a java compiler though.
                • 5. Re: Class.foName() and classname.class
                  807603
                  AnyClass{} is a class declaration in java. This is how you declare a valid java file with .java as extension.
                  And by this I think the OP really meant "suppose I define a class named AnyClass". I think he really meant to write
                  class AnyClass {
                  }
                  He did not mean for you to compare and contrast it with Java expressions AnyClass.class and Class.forName("AnyClass")