Solved

package directory question

Posted on 2011-03-25
22
392 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:gudii9
  • 10
  • 7
  • 4
  • +1
22 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 75 total points
ID: 35220274
when the compiler looks for a class it uses the package to determine what directory it is in
P1 in that example would need to be moved into a MyPackage directory

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35220282

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


0
 
LVL 47

Expert Comment

by:for_yan
ID: 35220283
If you want to compile only P1.java then you should say:

default_folder>javac MyPackage/P1.java

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35220291
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)
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 75 total points
ID: 35221297
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 !!
0
 
LVL 7

Author Comment

by:gudii9
ID: 35368922
>>P1 in that example would need to be moved into a MyPackage directory

you mean p2 right. please advise
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35369016
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
0
 
LVL 92

Expert Comment

by:objects
ID: 35370362
> 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
0
 
LVL 7

Author Comment

by:gudii9
ID: 35470774
>>>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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35470850

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.




0
 
LVL 92

Expert Comment

by:objects
ID: 35471556
> 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)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Author Comment

by:gudii9
ID: 35751178
>> 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
0
 
LVL 7

Author Comment

by:gudii9
ID: 35751181
i see

>>>

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


does this means p1 is in  package MyPackage; or not. please advise
0
 
LVL 92

Expert Comment

by:objects
ID: 35751194
> 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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35751195
Yes this means that class P1 is in package MyPackage

T
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35751200
As P1 is in the package MyPackage, so the file P1.java should be
MyPackage folder
0
 
LVL 7

Author Comment

by:gudii9
ID: 35935891
>>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
0
 
LVL 7

Author Comment

by:gudii9
ID: 35935906
>>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
0
 
LVL 7

Author Comment

by:gudii9
ID: 35935910
>>>> 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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35935915
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


 



0
 
LVL 47

Expert Comment

by:for_yan
ID: 35935932
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


0
 
LVL 47

Accepted Solution

by:
for_yan earned 350 total points
ID: 35935954


So once again, let's think we are sitting in certain default folder.

So your file P1.java with the package statement at the top should be resisnding in

the folder MyPackage:

MyPackage/P1.java


File P2.java should have import statement

import MyPackage.P1;

and file P2.java which does not have pacaege on top,
should reside in the default folder (the same one which as folder MyPackage

With these condtions if you are sitting in default folder
you can run

javac P2.java

it should lookinto MyPackage fodler for P1.java
and it should compile both of them.

If you have method main() in P2.java

you can then execute your program

java P2
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now