Connecting Tech Pros Worldwide Forums | Help | Site Map

getClass()

Needs Regular Fix
 
Join Date: Jan 2007
Posts: 281
#1: Apr 11 '07
What is the correct sytanx for
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. Class whatClass;
  2. if (whatClass == BigInteger.class) 
  3. { do something ....}
Please share the correct syntax with me....Thank You

Newbie
 
Join Date: Nov 2006
Posts: 30
#2: Apr 11 '07

re: getClass()


See this example:

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

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

you can use this to know the classname
Needs Regular Fix
 
Join Date: Jan 2007
Posts: 281
#3: Apr 11 '07

re: getClass()


Quote:

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.println(s.getClass());
System.out.println(obj.getClass());
}
}

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:
Expand|Select|Wrap|Line Numbers
  1. if (className == jonmath.java.math.MyBigInteger) 
  2. { //I need to do something only for this class.
  3.   //Do I need to initialize it? something like this?
  4.  // Class intClass = jonmath.java.math.MyBigInteger.class;
  5. }
  6.  
Please help..
prometheuzz's Avatar
Editor
 
Join Date: Apr 2007
Location: Rotterdam, the Netherlands.
Posts: 189
#4: Apr 11 '07

re: getClass()


Quote:

Originally Posted by shana07

thank you...
One more thing can I get something like this:

Expand|Select|Wrap|Line Numbers
  1. if (className == jonmath.java.math.MyBigInteger) 
  2. { //I need to do something only for this class.
  3.   //Do I need to initialize it? something like this?
  4.  // Class intClass = jonmath.java.math.MyBigInteger.class;
  5. }
  6.  
Please help..

You could use
Expand|Select|Wrap|Line Numbers
  1. if (className instanceof jonmath.java.math.MyBigInteger) {
  2.     // ...
  3. }
But if your code is going to be full of instanceof operators, you definatelly have a design flaw.
Needs Regular Fix
 
Join Date: Jan 2007
Posts: 281
#5: Apr 11 '07

re: getClass()


Quote:

Originally Posted by prometheuzz

You could use

Expand|Select|Wrap|Line Numbers
  1. if (className instanceof jonmath.java.math.MyBigInteger) {
  2.     // ...
  3. }
But if your code is going to be full of instanceof operators, you definatelly have a design flaw.

by doing that - compiler gives error: inverconvertible types
found : java.lang.Class
required: jonmath.java.math.MyBigInteger
Thanks for your reply....
prometheuzz's Avatar
Editor
 
Join Date: Apr 2007
Location: Rotterdam, the Netherlands.
Posts: 189
#6: Apr 12 '07

re: getClass()


Quote:

Originally Posted by shana07

by doing that - compiler gives error: inverconvertible types
found : java.lang.Class
required: jonmath.java.math.MyBigInteger
Thanks for your reply....

Ah, I see. Run this demo to see how the instanceof operator is used:
Expand|Select|Wrap|Line Numbers
  1. public class InstanceofDemo {
  2.  
  3.     public static void main(String[] args) {   
  4.         String s = "a string";
  5.         Integer i = new Integer(1);  
  6.         checkWithInstanceof(s);
  7.         checkWithInstanceof(i);
  8.         checkWithInstanceof(String.class);
  9.         checkWithInstanceof(null);
  10.     }
  11.  
  12.     public static void checkWithInstanceof(Object o) {
  13.         if(o instanceof String) {
  14.             System.out.println("'"+o+"' is a String.");
  15.         } 
  16.         else if(o instanceof Integer) {
  17.             System.out.println("'"+o+"' is a Integer.");
  18.         } 
  19.         else if(o instanceof Class) {
  20.             System.out.println("'"+o+"' is a Class.");
  21.         } 
  22.         else {
  23.             System.out.println("'"+o+"' is unknown.");
  24.         }
  25.     }
  26. }
Reply