getClass()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    getClass()

    What is the correct sytanx for
    Code:
    getClass()?
    I don't know how to use it as I read java doc it is something like Class<? extends Object> ..what does it mean?
    I need to know what class is that and need to do something like this
    Code:
    Class whatClass;
    if (whatClass == BigInteger.class) 
    { do something ....}
    Please share the correct syntax with me....Thank You
  • crossroadsk
    New Member
    • Nov 2006
    • 30

    #2
    See this example:

    class GetTheClass
    {
    public static void main(String[] args)
    {
    Object obj = new Object();
    String s = new String();
    System.out.prin tln(s.getClass( ));
    System.out.prin tln(obj.getClas s());
    }
    }

    It returns the classname of the object u used to call that method.

    you can use this to know the classname

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by crossroadsk
      See this example:

      class GetTheClass
      {
      public static void main(String[] args)
      {
      Object obj = new Object();
      String s = new String();
      System.out.prin tln(s.getClass( ));
      System.out.prin tln(obj.getClas s());
      }
      }

      It returns the classname of the object u used to call that method.

      you can use this to know the classname
      thank you...
      One more thing can I get something like this:
      Code:
      if (className == jonmath.java.math.MyBigInteger) 
      { //I need to do something only for this class.
        //Do I need to initialize it? something like this?
       // Class intClass = jonmath.java.math.MyBigInteger.class;
      }
      Please help..

      Comment

      • prometheuzz
        Recognized Expert New Member
        • Apr 2007
        • 197

        #4
        Originally posted by shana07
        thank you...
        One more thing can I get something like this:
        Code:
        if (className == jonmath.java.math.MyBigInteger) 
        { //I need to do something only for this class.
          //Do I need to initialize it? something like this?
         // Class intClass = jonmath.java.math.MyBigInteger.class;
        }
        Please help..
        You could use
        Code:
        if (className instanceof jonmath.java.math.MyBigInteger) {
            // ...
        }
        But if your code is going to be full of instanceof operators, you definatelly have a design flaw.

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #5
          Originally posted by prometheuzz
          You could use
          Code:
          if (className instanceof jonmath.java.math.MyBigInteger) {
              // ...
          }
          But if your code is going to be full of instanceof operators, you definatelly have a design flaw.
          by doing that - compiler gives error: inverconvertibl e types
          found : java.lang.Class
          required: jonmath.java.ma th.MyBigInteger
          Thanks for your reply....

          Comment

          • prometheuzz
            Recognized Expert New Member
            • Apr 2007
            • 197

            #6
            Originally posted by shana07
            by doing that - compiler gives error: inverconvertibl e types
            found : java.lang.Class
            required: jonmath.java.ma th.MyBigInteger
            Thanks for your reply....
            Ah, I see. Run this demo to see how the instanceof operator is used:
            Code:
            public class InstanceofDemo {
            
                public static void main(String[] args) {   
                    String s = "a string";
                    Integer i = new Integer(1);  
                    checkWithInstanceof(s);
                    checkWithInstanceof(i);
                    checkWithInstanceof(String.class);
                    checkWithInstanceof(null);
                }
                
                public static void checkWithInstanceof(Object o) {
                    if(o instanceof String) {
                        System.out.println("'"+o+"' is a String.");
                    } 
                    else if(o instanceof Integer) {
                        System.out.println("'"+o+"' is a Integer.");
                    } 
                    else if(o instanceof Class) {
                        System.out.println("'"+o+"' is a Class.");
                    } 
                    else {
                        System.out.println("'"+o+"' is unknown.");
                    }
                }
            }

            Comment

            Working...