473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

getClass()

281 100+
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
Apr 11 '07 #1
5 5054
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
Apr 11 '07 #2
shana07
281 100+
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..
Apr 11 '07 #3
prometheuzz
197 Expert 100+
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.
Apr 11 '07 #4
shana07
281 100+
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....
Apr 11 '07 #5
prometheuzz
197 Expert 100+
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. }
Apr 12 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Fred | last post by:
Are primitives objects? int i = 3; System.out.println(i.getClass()); doesn't compile. Get an error message "int can't be dereferenced" But yet the docs for class Object say: Class Object...
1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
3
by: enki | last post by:
I have been doing some reading on dynamic bindind and polymorphism. I know the basics from what I have seen in books. The way virtual methods were explained was that they were used when methods...
0
by: Niklas Pettersson | last post by:
A deffinite bug in mscorwks.dll makes my managed service crash abruptly. When hooking up windbg it turns out to be an "access violation" in the mscorwks assembly memory space. Every time it happens...
2
by: Philipp | last post by:
Hello This seems a simple question, please send me to the right FAQ if I missed it: If there is a class Fruits with two classes Apples and Bananas extending Fruits. Apples has a method called...
4
by: cmk128 | last post by:
Hi How can i do something like Java: if (str.getClass()==String.class){ } i found that, this syntax has error in g++: if (typeof(a) == typeof(b)){
10
by: mistb2002 | last post by:
I am trying to modify my program to add and delete inventory items. I tried just adding but I am getting mutliple errors help. This is what my program currently looks like // Display The...
8
by: shreedhan | last post by:
Hi, I'm very new to java... and I'm having a problem in getting a URL URL url; url=getClass().getClassLoader().getResource(name); System.out.println(url); This code works well when I supply...
1
by: desturrr | last post by:
I am having diffuculties while trying to make the path of my files relative. I am using an interface to hold my static variables in order not to mix them with my other codes , just implementing the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.