java编译如何在另一个类中创建一个类的对象? [英] How java compiles creates objects of one class in another class?
问题描述
我是Java的新手,遇到了一个问题,即java编译器如何在另一个类中创建一个类的对象。例如:
我的A级如下:
class A {}
// 我有B级主要方法:
class B {
public static void main( String [] args){
a a = new A();
System.out.println(a);
}
}
这是一个问题:我在一个单独的txt文件中编写了A类,在另一个txt文件中编写了B类。当我编译 B类
编译器如何知道类A
存在,因为没有的痕迹
。我们没有给出 B类
中的A类 A类
的路径,我们并没有给出 A类
存在的任何线索。但是java编译器如何知道并在 class B
中为 Class A
创建对象。这可能是一个愚蠢的问题,但它没有让我在没有答案的情况下继续前进。提前致谢。
我的尝试:
我有A级如下:
class A {}
我有B级主要方法:
B级{
public static void main(String [] args){
A a =新A();
System.out.println(a);
}
}
编译B时,Java编译器会尝试查找所有关联的类,以便创建可运行的程序。如果它找不到A.class文件,但它可以找到A.java文件,那么它将自动构建该类。尝试运行命令
java -verbose B.java
您将看到它的实际效果。
I am completely new to Java and came across a question that how java compiler creates object of one class in another class. For example :
I have class A as below :
class A{}
//I have class B with main method :
class B{
public static void main(String[] args){
A a = new A();
System.out.println(a);
}
}
Here is the question: I wrote class A in one separate txt file and class B in another txt file. When I compile Class B
How compiler knows that class A
exists since there is no trace of class A
in class B
. We are not giving path of Class A
and we are not not giving any clue that class A
exists. But how java compiler knows and create object for Class A
in class B
. This might be a silly question but it is not letting me to proceed further without an answer. Thanks in advance.
What I have tried:
I have class A as below :
class A{}
I have class B with main method :
class B{
public static void main(String[] args){
A a = new A();
System.out.println(a);
}
}
When you compile B then the Java compiler tries to find all associated classes in order to create a runnable program. If it cannot find the A.class file, but it can find the A.java file then it will build the class automatically. Try running the command
java -verbose B.java
and you will see it in action.
这篇关于java编译如何在另一个类中创建一个类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!