java编译如何在另一个类中创建一个类的对象? [英] How java compiles creates objects of one class in another class?

查看:1055
本文介绍了java编译如何在另一个类中创建一个类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆