什么是MyClass.class? [英] what is MyClass.class?
问题描述
以下是MainClass.java中列出的代码。
following is the code listed in MainClass.java.
public class MainClass {
public static void main(String[] args) {
System.out.println("main started...");
Class c = MyClass.class ;
//this class variable seems to be public static.
//But, as it is clearly visible in the MyClass,
//no reference variable is declared.
//My problem is that from where this class variable
//came from.
//i also check out the Object.java file, but it also don't
//have any public static class variable of Class class
//like there is
//out (instance of PrintStream class) in System class.
//Hope all u mindoverflow guys help me to sort out
//this probz.
try {
Class.forName( c.getName() ) ;
System.out.println("classloader of MyClass : " + MyClass.class.getClassLoader());
System.out.println("classloader of MyClass : " + String.class.getClassLoader());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("main ended...");
}
}
class MyClass{
static{
System.out.println("static block of MyClass class.");
}
}
thnx coobird ...
i找到了文章非常有用。 :)
thnx coobird... i found the article quite useful. :)
但是,关于文学作品,我的知识仅限于:
But, about litereals my knowledge is only limited to:
int i = 5 ; //here 5 is an integer literal
float f = 5.6f ; //here 5.6f is a float literal
唯一的非原始文字,我知道是
the only non-primitive litereal, i know is
String str = "java" ; //"java" is a String litereal
和类文字,你和Jon Skeet明确表示我非常好。
and class literal, which u and Jon Skeet make clear to me very well.
在java中找到更多的文字???
同意...
因此根据讨论,总文字归类为: -
agreed... so as per the discussion, total literals are categorized as:-
- 原始文字
- 字符串文字
- class literal
- null
- primitive literals
- string literals
- class literal
- null
还有更多的文字(为了让列表更长一点:))
are there some more literals (to make the list a little longer :) )
当我使用decomipler反编译MainClass.class时,两个Class
类型的静态变量(可能是因为,我使用过发现类文字2次)被自动添加,但从未在代码中找到使用
。此外,两个类文字都直接替换为我在java文件中使用它们的类文件。
when i decompile the MainClass.class using decomipler, two Class type static variables (may be coz, i have used class literal 2 times) are found to be automatically added, but never found to be used in the code. Also, both the class literals are directly replaced from the class file where i have used them in the java file.
My代码: -
public class MainClass {
public static void main(String[] args) {
System.out.println("main started...");
Class c = MyClass.class ;
try {
Class.forName( c.getName() ) ;
System.out.println("classloader of MyClass : " + MyClass.class.getClassLoader());
System.out.println("classloader of MyClass : " + String.class.getClassLoader());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("main ended...");
}
}
反编译生成的代码: -
import java.io.PrintStream;
public class MainClass
{
public MainClass()
{
}
public static void main(String args[])
{
System.out.println("main started...");
Class c = MyClass;
try
{
Class.forName(c.getName());
System.out.println((new StringBuilder("classloader of MyClass : ")).append(MyClass.getClassLoader()).toString());
System.out.println((new StringBuilder("classloader of MyClass : ")).append(java/lang/String.getClassLoader()).toString());
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
System.out.println("main ended...");
}
static Class class$0;
static Class class$1;
}
推荐答案
这是类文字 - 获取特定类型的类< T>
的简单方法。
It's "class literal" - a simple way of getting the Class<T>
for a particular type.
参见 Java语言规范的第15.8.2节以获取更多详细信息。
See section 15.8.2 of the Java Language Specification for more details.
请注意,它不是该类的字段,它是一种特殊的语法糖。
Note that it's not a "field" of the class, it's a special piece of syntactic sugar.
由于类型擦除,您可能会遇到有关泛型的有趣限制。 TypeLiteral < Guice中引入的/ code>
提供了更多信息和解决方法。
Due to type erasure, you may run into interesting restrictions around generics. The TypeLiteral
introduced in Guice gives more information and a workaround.
在实现方面,它取决于您要定位的字节码版本。如果您使用 -target 1.4
(或以下),则会在代码中插入对 Class.forName()
的调用在类型初始化期间调用的静态方法中。如果使用 -target 1.5
(或以上),常量池将获得类条目。我不知道如何处理这个细节。
In terms of implementation, it depends on which bytecode version you're targeting. If you use -target 1.4
(or below), a call to Class.forName()
is inserted into your code in a static method which is called during type initialization. If you use -target 1.5
(or above) the constant pool gets a "class" entry. I don't know the details of how this is handled though.
这篇关于什么是MyClass.class?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!