Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

package directory question

Hi,

I was going through following question from  site
http://www.jchq.net/certkey/0102certkey.htm


Question 9)What happens when you attempt to compile and run these two files in the same directory?
//File P1.java
package MyPackage;
class P1{
void afancymethod(){
        System.out.println("What a fancy method");
        }
}
//File P2.java
public class P2 extends P1{
    afancymethod();
}

Answer 9)4) P1 compiles cleanly but P2 has an error at compile time

Even though P2 is in the same directory as P1, because P1 was declared with the package statement it is not visible from P2

I have not understood answer. does directory and package both refer same. please advise. thanks in advance
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

if you declare package then you should place the file in the appropriate folder

in you case you should have class P2 in the dafult folder
and class P1 should reside in MyPackage folder.

so if you place

deafault_foldeer>P2.java
default_)folder\MyPackage>P1.java

then

default_folder>javac P2.java

will complie P2 then compiler will find depndecy on P1
find its packege and the it will fgo itno folder and compile P1.java also


If you want to compile only P1.java then you should say:

default_folder>javac MyPackage/P1.java

Let's supopose that P1.java conatins main method - it should start execution,
then to run the program after compilation uyou'll have to say:

java MyPackage.P1

(using dot beween package and class name)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

>>P1 in that example would need to be moved into a MyPackage directory

you mean p2 right. please advise
No, I'm sure, objects meant P1 in that remark, becuase it is in the P1.java file, that you write
at the top

package MyPackage;

therefore P1.java should reside in the MyPackage folder.

P2.java does not have package at the top - so it should reside
in default folder, just where you compile it
> you mean p2 right. please advise

no I meant P1. As it is declared as being in the MyPackage package it is best practice to store it in a MyPackage directory
Avatar of gudii9

ASKER

>>>no I meant P1. As it is declared as being in the MyPackage package it is best practice to store it in a MyPackage directory

P1 is already in  package MyPackage right as in the code.


//File P1.java
package MyPackage;
class P1{
void afancymethod(){
        System.out.println("What a fancy method");
        }
}
//File P2.java
public class P2 extends P1{




what is the location of class P2. I was not clear on that either.please advise

Required location of the file depends on its package as declared on
top of the file; it does not depend on from which class it inherits.

Therefore if you are sitting in defaulty directory, then starting from this dierectory
and if classes P1 and P2 are in different files, then
file  P1.java should be sitting in
the folder MyPackage (because file has "package MyPackage;" at the top).
file P2.java does not have package declared at the top - so it should
be in the default directory itsels (one level higher that class P1.java);
it doesn't matter that P2 extends P1 - they stuill need to be in different folders
and in this case P2 is one level higer than P1, as P2.java does not have package at the top of the file.




> P1 is already in  package MyPackage right as in the code.

I said it need to be moved into a MyPackage *directory*.

> what is the location of class P2. I was not clear on that either.please advise

P2 is not in a package. It is in what is called the default package (which should rarely be used)
Avatar of gudii9

ASKER

>> P1.java should be sitting in
the folder MyPackage (because file has "package MyPackage;" at the top).
file P2.java does not have package declared at the top - so it should
be in the default directory itsels (one level higher that class P1.java);
it doesn't matter that P2 extends P1 - they stuill need to be in different folders
and in this case P2 is one level higer than P1, as P2.java does not have package at the top of the file.



what you mean by top, bottom. I was not clear. Does p1 which is in MyPackage needs to be moved to default package which already has p2.java or reverse. please advise
Avatar of gudii9

ASKER

i see

>>>

//File P1.java
package MyPackage;
class P1{


does this means p1 is in  package MyPackage; or not. please advise
> Does p1 which is in MyPackage needs to be moved to default package which already has p2.java or reverse.

you can do either. the point of the questiion is that the classes are not in the correct directory to match the package structure. To fix it you need to ensure the right packages are in the correct directory. Otherwise the compiler will be unable to find the class (it uses the package name to look them up)

> does this means p1 is in  package MyPackage; or not. please advise

yes it does
Yes this means that class P1 is in package MyPackage

T
As P1 is in the package MyPackage, so the file P1.java should be
MyPackage folder
Avatar of gudii9

ASKER

>>As P1 is in the package MyPackage, so the file P1.java should be
MyPackage folder

i was not clear on difference between P1 and P.java are both not the same. please advise
Avatar of gudii9

ASKER

>>class P1 is using default modiffer so it will used in the same package only not outside !!


change the P1 class as public and try !!

sane oackage means MyPackage. please advise
Avatar of gudii9

ASKER

>>>> P1 is already in  package MyPackage right as in the code.

I said it need to be moved into a MyPackage *directory*.

> what is the location of class P2. I was not clear on that either.please advise

P2 is not in a package. It is in what is called the default package (which should rarely be used)



what is difference between package and directory. I am more confused. please advise
I didn't see and P.java in your question

You had file P1.java
this file starts with

package MyPackage;

This measn that it should be located inside the folder MyPackage.
So you need to creatte such folder and move file P1.java to that folder

file P2.java does not have package in top of it.
it is in the defalut package.

In order to recognise P1 which it extends, you need
to put

import MyPackage.P1;

on top of thes file

Then you cna go to your default folder
(within which you have folder MyPackage which contains P1.java;
and the file P2.java).

After that you can compile

javac P2.java

and it shoul fine P1.java inside MyPcakage because of
import statemment


 



Package is a Java terminology,

Duirectory is a general terminology fo file system.

Java was designed in suh  a way, taht if your class belongs to the package
it should be placed in corresponding folder structure.

So if I have at the top of my file MyClass.java

package com.mycompany.app;
 public class MyClass {...


it means that the file MyClass.java should reside in the folder com/mycompany/app
that is where compiler would be looking for it and where java virtual machine will be looking for the
compiled class


ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial