What are types in Java ?
When it's good practice to use var instead of explicit name of the variable
Videos
a.getClass().getName()
Expanding on Martin's answer...
Martins Solution
a.getClass().getName()
Expanded Solution
If you want it to work with anything you can do this:
((Object) myVar).getClass().getName()
//OR
((Object) myInt).getClass().getSimpleName()
In case of a primitive type, it will be wrapped (Autoboxed) in a corresponding Object variant.
Example #1 (Regular)
private static String nameOf(Object o) {
return o.getClass().getSimpleName();
}
Example #2 (Generics)
public static <T> String nameOf(T o) {
return o.getClass().getSimpleName();
}
Note: Anything below here does not directly answer the posed question. I added the following examples to provoke thought in those who are potentially looking for variable class names for one reason or another.
Getting the Instance using Pattern Matching
If statement (as of Java 17)
if(object instanceof String str)
System.out.println(str);
Switch statement (as of Java 21)
switch(object) {
case null -> System.out.println("The provided object is null.");
case String str -> System.out.println("String encountered. The value is: " + str);
case Integer myInt when myInt > 0 -> System.out.println("Integer greater than 0 encountered. The value is: " + myInt);
case Integer myInt when myInt <= 0 -> System.out.println("Integer less than or equals to 0 encountered. The value is: " + myInt);
default -> System.out.println("Unexpected Object encountered.");
}
Totally unnecessary and over the top example
Do with this what you will, but don't take the example as gospel. I am only showing possibilities here and you should make your own judgements on how to use these newer features.
public static class MyObject {
public final String myField;
public MyObject(String myField) { this.myField = myField; }
@Override public String toString() { return "{ myField: " + this.myField + "}"; }
}
public static record MyRecord(String myField) {
@Override public String toString() { return "{ myField: " + this.myField + "}"; }
}
public <T> String example(T object) {
return switch(object) {
case null -> "NULL";
case String str -> "String: " + str;
case Integer iNt -> "Integer: " + iNt;
case Long lOng -> "Long: " + lOng;
case MyObject myObject -> MyObject.class.getSimpleName() + ": " + myObject.toString();
case MyRecord myRecord -> myRecord.getClass().getSimpleName() + ": " + myRecord.toString();
default -> "UNKNOWN";
};
}
More reading about pattern matching in switch statements: https://openjdk.org/jeps/441
Additional Learning
- Material on Java Types, Values and Variables: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html
- Autoboxing and Unboxing: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
- Docs on Pattern Matching for instanceof: https://docs.oracle.com/en/java/javase/14/language/pattern-matching-instanceof-operator.html
- A good talk about history of some of these things: https://www.youtube.com/watch?v=wo84LFzx5nI