java中的.class文件
谁能指导我解决这个问题(记住这个问题是在 Java Reflection 上下文中)
有一个名为“A”的java类,它有一个名为“B”的内部类。当我编译这个java文件时,它创建了2个版本.class 文件的一个名为 "A.class" & “A$B.class”并且如果“A”类有2个内部类即B,C那么将创建 3 个版本“A.class” & “A$B.class”& “A$C.class”
为什么会发生这种情况。如果我想读取 .class 文件,那么当我给出 A.class 时,我读取 b/c 的 .class 文件,那么它会要求 A$B.class 并给出 引起原因:java.lang.ClassNotFoundException:A$B.class
如果我们给出 A$B.class 那么它会给出异常
java.lang.NoClassDefFoundError:
java.lang.ClassNotFoundException
class A {
String _name="Tom";
class B{
int _phoneNo=8384040;
}
}
Can anyone guide me in this problem (Remember this problem is under Java Reflection context)
There are is a java 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此您有两个类,并且应该有两个类文件。
您有三个类和三个类文件。
当您读取 A.class 文件时,您只是在读取文件。它不会要求你做任何其他事情。
仅当此类不在您的类路径中时才会发生这种情况,这是完全不同且不相关的事情。
这是加载类的方式,但创建这些类的实例。不需要读取底层类,甚至不需要知道它们在磁盘上的排列方式。
So you have two classes and you should have two class files.
You have three classes and three class files.
When you read the A.class file, you are just reading a file. It doesn't ask you for anything else.
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$1”、“A@2”等。Java
中名为“AB”的类总是存储在名为 A$B.class 的文件中。如果您出于某种原因需要查找类文件,那么您应该在其中查找。但在反射调用中,您仍然将类称为“AB”,而不是“A$B”。
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".