j******r 发帖数: 201 | 1 how to dynamically get the full class name? using reflect?
for example, from "String" get "java.lang.String" |
xt 发帖数: 17532 | 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 发帖数: 201 | 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 发帖数: 17532 | 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 发帖数: 201 | 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 发帖数: 17532 | 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 发帖数: 201 | 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 发帖数: 11 | 8 String.class.getName()
【在 j******r 的大作中提到】 : En, : Thank you.
|
qm 发帖数: 27 | 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.
|