if (obj instanceof C) {
//your code
}
Answer from IAdapter on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › reflect › class › classNew.html
Retrieving Class Objects (The Java™ Tutorials > The Reflection API > Classes)
If an instance of an object is available, then the simplest way to get its Class is to invoke Object.getClass(). Of course, this only works for reference types which all inherit from Object. Some examples follow. Class c = "foo".getClass(); Returns the Class for String · Class c = ...
Discussions

How do i check if an object is an instance of a class while having the name of that class passed as a parameter?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
7
3
November 21, 2022
How to Get Type's Class of a Collection - Oracle Forums
Hi all, I just wanna ask if there is a simple way to get the type's Class of a Collection. For example, for an ArrayList or List objects, how do I dynamically get "S... More on forums.oracle.com
🌐 forums.oracle.com
January 25, 2012
[Java] How do I get the name of the class that called my object's constructor?
You can examine the stack for the current thread to find the caller. E.g.: Thread currentThread = Thread.currentThread(); StackTraceElement[] stackTrace = currentThread.getStackTrace(); for (StackTraceElement element : stackTrace) { System.out.println(element); } This will give you the name for the calling class and method. You can use reflection to achieve the other things you are looking for, I think. Though I'm curious what problem you are trying to solve? Might be a better approach if you are willing to provide more context More on reddit.com
🌐 r/AskProgramming
13
2
November 16, 2024
How can I get the generic type of a List from a Class instance?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
11
1
May 11, 2023
🌐
Baeldung
baeldung.com › home › java › core java › finding an object’s class in java
Finding an Object’s Class in Java | Baeldung
February 17, 2025 - In this article, we’ll explore the different ways of finding an object’s class in Java. The first method that we’ll check is the getClass() method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › object-class-in-java
Object Class in Java - GeeksforGeeks
The Object class is the root class of the Java class hierarchy. Every Java class directly or indirectly inherits from the Object class. Belongs to the java.lang package and is automatically available in every Java program. Provides common methods such as toString(), equals(), hashCode(), and getClass().
Published: 3 weeks ago
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Object.html
Object (Java Platform SE 8 )
July 21, 2026 - Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class. The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called.
🌐
CodeGym
codegym.cc › java blog › methods in java › java object getclass() method
Java Object getClass() Method
May 30, 2022 - Java uses a method called getClass() provided by the class Object to get the class of any object used.
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_classes.asp
Java Classes and Objects
A Class is like an object constructor, or a "blueprint" for creating objects. To create a class, use the keyword class. In this example, we create a class named "Main" with a variable x: ... Remember from the Java Syntax chapter that a class should always start with an uppercase first letter, and that the name of the java file should match the class name.
🌐
Dev.java
dev.java › learn › reflection › classes
Retrieving Classes - Dev.java
July 19, 2024 - None of the classes in java.lang.reflect that you need to use to get reflective access to any element of a class have public constructors. The java.lang.reflect.ReflectPermission class is an exception, but this class is now deprecated. To get to these classes, it is necessary to invoke appropriate methods on Class. There are several ways to get a Class depending on whether the code has access to an object, the name of class, a type, or an existing Class.
🌐
Coderanch
coderanch.com › t › 404450 › java › type-object
how can I get the type of the object ? (Beginning Java forum at Coderanch)
hello if I have some variable and I like to know what is the stype of this variable if there any "typeOf" method that gives me that info? ... You can use getClass() to get the java.lang.Class object that represents the type of the object.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › object_getclass.htm
Java Object getClass() Method
The Java Object getClass() method returns the runtime class of an object.
🌐
Medium
vicksheet.medium.com › understanding-the-object-class-in-java-726a8bd8a885
Understanding the Object Class in Java | by Vicksheet Shanbhag | Medium
March 20, 2024 - The toString() method returns a string representation of the object. By default, it returns a string that contains the class name and the hashcode value of the object. However, you can override this method to provide a more meaningful string ...
🌐
Oracle
forums.oracle.com › ords › apexds › post › how-to-get-type-s-class-of-a-collection-7858
How to Get Type's Class of a Collection - Oracle Forums
January 25, 2012 - Hi all, I just wanna ask if there is a simple way to get the type's Class of a Collection. For example, for an ArrayList or List objects, how do I dynamically get "S...
🌐
Vultr Docs
docs.vultr.com › java › examples › determine the class of an object
Java Program to Determine the class of an object
December 12, 2024 - By the end of your reading, you'll be able to implement these methods in your Java applications to determine the dynamic type of objects. Utilize the getClass() method to retrieve the class of an object.
🌐
Scaler
scaler.com › home › topics › getclass() in java
getclass() in Java- Scaler Topics
June 7, 2022 - The getClass method returns the runtime class of the object "this". For example, suppose we call getClass() on an object obj (as demonstrated in code snippet above), the return value of the getClass() will be the class of which this is an object of. "this" in this case is our object obj itself.
🌐
Hyperskill
hyperskill.org › university › java › retrieving-class-instances-in-java
Retrieving Class Instances in Java
December 25, 2025 - The variable objectArrayClass in its turn will contain the Class corresponding to a two-dimensional array of Object. Note, that there should be a semicolon ; after an array of any objects. In addition to the methods we've described above, we can use some Reflection APIs to get classes.
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › reflect › class › getClass.html
Retrieving Class Objects
The Java · TM · Tutorial · Trail: ... an instance of the class is available, you can invoke Object.getClass. The getClass method is useful when you want to examine an object but you don't know its class....
🌐
Coderanch
coderanch.com › t › 370895 › java › class-instantiated-object
Getting a class name without an instantiated object (Java in General forum at Coderanch)
April 4, 2003 - Also to do anything to our instantiated classes, we still will need to typecast them to the proper type which can't be done with a String, we need to know the class at compile time. Michael Morris · Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher ... Hmm, I'm apparently not on the same page here. . Good hack Chris and James, I wouldn't have thought of trying that. It seems to show a deficiency in Reflection though: that one must resort to unconventional means to get the enclosing class from a static context.
🌐
Reddit
reddit.com › r/askprogramming › [java] how do i get the name of the class that called my object's constructor?
r/AskProgramming on Reddit: [Java] How do I get the name of the class that called my object's constructor?
November 16, 2024 -

I have class A with a String origin field and a getter for said field. I want A's constructor to do the following:

- Check whether the method where A got initialized (i.e. the method that called A's constructor) is static or not
- If it's static, initialize the origin field with the name of the class said method belongs to. If the method has been overridden down the hierarchy, I want the name of the class used to CALL the method, rather than the one that implemented it or the interface that declared it.
- If it's NOT static, initialize the origin field with the name of the whatever instance was used to call said method.

Is it possible to do something like this, and if so: how?

🌐
Baeldung
baeldung.com › home › java › core java › determine the class of a generic type in java
Determine the Class of a Generic Type in Java | Baeldung
August 26, 2026 - In Generics, the type of information is only available at compile-time. Java compiler erases type information during compilation and it’s not available at runtime. This is called type erasure. Due to type erasure, all the type parameter information is replaced with the bound (if the upper bound is defined) or the object type (if the upper bound isn’t defined). We can confirm this by using the javap utility, which inspects the .class file and helps to examine the bytecode.