how to dynamically get the full class name? using reflect? for example, from "String" get "java.lang.String"
xt
2 楼
getClass().getName()
【在 j******r 的大作中提到】 : how to dynamically get the full class name? using reflect? : for example, from "String" get "java.lang.String"
j*r
3 楼
no, I dont have the class. Only have the String "String". In other words, how to navigate the java class hierarchy.
【在 xt 的大作中提到】 : : getClass().getName()
xt
4 楼
You only have the result of the Object.toString()? Then you are helpless.
【在 j******r 的大作中提到】 : no, I dont have the class. Only have the String "String". : In other words, how to navigate the java class hierarchy.
j*r
5 楼
i am thinking in Java compiler, after you import the packages, You can define your variables only by using the "short" class name. For example: import java.net.*; ... Socket s = .... And the compiler knows that the "Socket" is "java.net.Socket". So there should be a way to find out the fully qualified name of "Socket".
【在 xt 的大作中提到】 : : You only have the result of the Object.toString()? Then you : are helpless.
xt
6 楼
I see. The trick is to search through all the import defined in the java file, plus package java.lang.*. When you use short name of classes, the name *must* be unique across the imported classes, or you will have compile time error. For example, it won't compile if you do: import java.sql.*; import java.util.*; ... Date date = new Date(); ... This is because the compiler cannot tell if it is java.sql.Date or java.util.Date. This is why for better programming styles, you should limit your import
【在 j******r 的大作中提到】 : i am thinking in Java compiler, after you import the packages, : You can define your variables only by using the "short" class name. : For example: : import java.net.*; : ... : Socket s = .... : And the compiler knows that the "Socket" is "java.net.Socket". : So there should be a way to find out the fully qualified name : of "Socket".
j*r
7 楼
En, Thank you.
【在 xt 的大作中提到】 : : I see. : The trick is to search through all the import defined in the java file, : plus package java.lang.*. When you use short name of classes, the name : *must* be unique across the imported classes, or you will have compile : time error. : For example, it won't compile if you do: : import java.sql.*; : import java.util.*; : ...
m*r
8 楼
String.class.getName()
【在 j******r 的大作中提到】 : En, : Thank you.
qm
9 楼
the following code can find out the full name if it's already loaded. but it fails if the class is not loaded. this question is not useful in practice. but it helps to understand some basic concepts. note that this code assumes the system default class loader is used. public class test { public static void main (String[] args) throws Exception { class MyClassLoader extends ClassLoader { public MyClassLoader (ClassLoader parent) { super(parent); } public String get
【在 j******r 的大作中提到】 : no, I dont have the class. Only have the String "String". : In other words, how to navigate the java class hierarchy.