0

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.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • The Java compiler actually looks on your hard drive to find these files. You can set the path(s) it uses to look for them manually with the `-classpath` option. http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html – markspace Aug 21 '16 at 02:49
  • Compiler does not create object, it just verify the reference type compatibility and availability of that class during compile time(here class A in class B). So class loader loads class A first then loads class B since class B depends on class A. Object creation happens at run time. If you understand compile time and run time actions separately then most of your doubt will be solved. ref: http://stackoverflow.com/questions/20796046/flow-of-class-loading-for-a-simple-program, http://www.javaworld.com/article/2077260/learn-java/learn-java-the-basics-of-java-class-loaders.html – dkb Aug 21 '16 at 02:56
  • -http://stackoverflow.com/questions/39060422/how-java-compiles-creates-objects-of-one-class-in-another-class] Thanks.. @dkb – Javabie Aug 21 '16 at 03:02
  • You *do* have to give a path to class `A` if you're not compiling it in the same module. – chrylis -cautiouslyoptimistic- Aug 21 '16 at 03:43

3 Answers3

1

When classes are in the same package, the compiler will automatically find them. Otherwise, you need to add an "import" expression. For example, import example.classA;

Julieta
  • 407
  • 2
  • 8
0

When you create a java object there's two main parts, "Creating the object" and "Making a reference to that object".
Compiler is checking only the reference part. That means it checks the type of reference (In your case type is of class A). So that it does not care whether object is created or not.
When java file is compiled it creates a .class file and compiler knows how to find and read a class file in a local disk.

Lasitha Yapa
  • 4,309
  • 8
  • 38
  • 57
  • [http://stackoverflow.com/questions/39060422/how-java-compiles-creates-objects-of-one-class-in-another-class] Thanks @Lashitha Yapa – Javabie Aug 21 '16 at 03:02
0

Source files written in Java language are compiled into .class files with a tool called javac.

There are two things that javac needs to run:

1) a list of source files to compile 2) a list of paths on your hard drive where other .class, or .java (or jars) files are located if you refer to them in your source code. This list of paths is called a classpath.

When you do not specify a classpath argument when invoking javac, javac uses your current directory to look for supporting .java or .class files (and if you have an environment variable called CLASSPATH, it looks there too).

In your example, when javac compiles ClassA, it sees that it relies on ClassB, and tries to look for ClassB.java in the same directory as ClassA.

If you are using an IDE to compile, your IDE takes care of filling in the arguments that javac needs to run.

You can read more about javac here: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html

jmrah
  • 5,715
  • 3
  • 30
  • 37