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 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
and class literal, which u and Jon Skeet make clear to me very well.
are there more literals found in java???
agreed... so as per the discussion, total literals are categorized as:-
- primitive literals
- string literals
- class literal
- null
are there some more literals (to make the list a little longer :) )
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 code :-
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...");
}
}
Decompiler generated code :-
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;
}