java中的.class文件

发布于 2024-10-27 11:05:26 字数 810 浏览 1 评论 0原文

谁能指导我解决这个问题(记住这个问题是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哥,最终变帅啦 2024-11-03 11:05:26

有一个名为“A”的 java 类,它有一个名为“B”的内部类

因此您有两个类,并且应该有两个类文件。

如果“A”类有 2 个内部类,即 B、C

您有三个类和三个类文件。

如果我想读取 .class 文件,那么当我给出 A.class 时,我读取 b/c 的 .class 文件,那么它会要求 A$B.class

当您读取 A.class 文件时,您只是在读取文件。它不会要求你做任何其他事情。

并给出 Caused by: java.lang.ClassNotFoundException: A$B.class

仅当此类不在您的类路径中时才会发生这种情况,这是完全不同且不相关的事情。


这是加载类的方式,但创建这些类的实例。不需要读取底层类,甚至不需要知道它们在磁盘上的排列方式。

 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'.

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'.
熊抱啵儿 2024-11-03 11:05:26

“为什么这样做”的答案是每个类都有自己的类文件:即使是匿名类也会保存为单独的类文件,名为“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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文