开发者

.class file in java

Can anyone guide me in this problem (Remember this problem is under Java Reflection context)

There are is a ja开发者_Python百科va class named "A" having an inner class named "B".When i compile this java file then it create 2 versions of .class file one named as "A.class" & "A$B.class" and If "A" class have 2 inner classes i.e B,C then there will be 3 version created "A.class" & "A$B.class" & "A$C.class"

Why this is happen.

And if i want to read the .class file then what .class file i read b/c when i gave A.class then it will ask for A$B.class and give Caused by: java.lang.ClassNotFoundException: A$B.class

And if we gave A$B.class then it will gave exception

java.lang.NoClassDefFoundError: 
java.lang.ClassNotFoundException


class A {
 String _name="Tom";
    class B{
        int _phoneNo=8384040;  
    }
}


There are is a java class named "A" having an inner class named "B"

So you have two classes and you should have two class files.

If "A" class have 2 inner classes i.e B,C

You have three classes and three class files.

And if i want to read the .class file then what .class file i read b/c when i gave A.class then it will ask for A$B.class

When you read the A.class file, you are just reading a file. It doesn't ask you for anything else.

and give Caused by: java.lang.ClassNotFoundException: A$B.class

This will only happen if this class is not in your class path, which is a completely different and unrelated thing.


This is how you load the classes, but creating instances of those classes. There is no need to read the underlying classes, or even know how they are arranged on disk.

 A a = new A(); // creates an instance of A
 A.B b = a.new A.B(); // creates an instance of A.B which is "inside" 'A a'.


The answer to "why it does this" is that every class gets its own class file: even anonymous classes are saved as separate class files named "A$1", "A@2", etc.

The class named "A.B" in Java is always going to be stored in a file named A$B.class . If you need to find a class file for some reason, that's where you should look. But in reflection calls, you still call the class "A.B", not "A$B".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜