java - java中的.class文件

标签 java reflection

任何人都可以指导我解决这个问题(记住这个问题是在 Java Reflection 上下文中)

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

如果我们给出 A$B.class 那么它会给出异常

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"

所以你有两个类,并且应该有两个类文件。

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

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

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

当你读取A.class文件时,你只是在读取一个文件。它不会要求您提供任何其他信息。

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

关于java - java中的.class文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5459774/

相关文章:

java - Java中的super()

java - 如何使 Jersey Rest POST 请求同步

Java HotSpot(TM) 客户端共享与不共享

java - 如何使用 Postman 从curl 发布 REST

java - 使用装饰器模式时找到特定类型的装饰器?

reflection - 不能在用反射制作的 slice 上使用范围,然后传递 json.Unmarshal

c# - 如何从对包含类型的引用中获取 ICollection<> 属性的名称?

c# - 将代码动态附加到方法 .Net-Core

Java:从不同类加载器中的类调用方法

java - 如何在获得许可后重新加载 Activity