以下是MainClass.java中列出的代碼。什麼是MyClass.class?
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 ... 我發現文中非常有用。 :)
但是,關於litereals我的知識僅限於:
int i = 5 ; //here 5 is an integer literal
float f = 5.6f ; //here 5.6f is a float literal
唯一的非原始litereal,我所知道的是
String str = "java" ; //"java" is a String litereal
和類文本,其中U和喬恩斯基特對我很清楚。
是否還有更多的文字在Java中找到?
同意...... 使每討論,總面值分爲以下幾類: -
- 原始文字
- 字符串文字
- 類文本
- 空
是有一些文字(上榜的時間稍長:))
當我使用decomipler反編譯MainClass.class,二 類類型的靜態變量(可以!因爲,我已使用類文字2次)被發現自動添加,但從未發現在代碼中使用 。此外,這兩個類文字直接從我在java文件中使用它們的類文件中替換。
我的代碼: -
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;
}
null怎麼樣? – 2009-06-29 13:23:13
true和false以及字符文字(char c ='\ n')怎麼樣?請參閱http://java.sun.com/docs/books/jls/third_edition/html/lexical .html#3.10爲文字類型的完整列表。 – 2009-06-29 16:58:33
但列表沒有類字面值,爲什麼這樣? – mogli 2009-06-29 18:03:27