About Question com.enthuware.scja.v1.2.67 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.scja.v1.2.67 :

Post by ETS User »

I don't understand this question. If A.class is in a package, doesn't A.java have to be in the same
package? If so, how can A.java be compiled from
home and A.class be in classes\com? I thought
a java file with a package statement had to be
compiled from the directory specified in the
package statement.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.scja.v1.2.67 :

Post by admin »

Hello,

From your question, you might have a couple of misunderstandings:

1. Package statement applies to class files, not java files. A java file can be anywhere. The package statement in the java file has no bearing on the location of the java file. So, you can, for example, put Hello.java containing package test; in say c:\temp\

Although, in practice, you should organize your java files in the directory structure that matches the package name structure.

2. You can compile any java file kept in any directory from any directory and direct the resulting class file in any directory. For example:

c:\>javac -d c:\temp\classes c:\works\java\*.java

Here,
c:\> is your current directory
c:\works\java\*.java is where all your java files are.
c:\temp\classes is where your class files will go along with their package matching directory structure. So if your Test.java file has package a.b.c; The class file will be created in c:\temp\classes\a\b\c\Test.class

Note that the package structure is created in c:\temp\classes because of the -d option of javac. As an exercise, try the same command without specifying -d and see what happens.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 90 guests