avatar
get full class name# Java - 爪哇娇娃
j*r
1
how to dynamically get the full class name? using reflect?
for example, from "String" get "java.lang.String"
avatar
xt
2

getClass().getName()

【在 j******r 的大作中提到】
: how to dynamically get the full class name? using reflect?
: for example, from "String" get "java.lang.String"

avatar
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()

avatar
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.

avatar
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.

avatar
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".

avatar
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.*;
: ...

avatar
m*r
8
String.class.getName()

【在 j******r 的大作中提到】
: En,
: Thank you.

avatar
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。