• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

doubt in Development

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am little confuse about chapter-10,self test example no:5,page no:783. They have given answer "C". But I think the answer is "B". Can anybody who has book help me?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dolly,

You need to compile B.java that is in the package xcom. class B needs class A because it extends class A. Class A and B are in the same package xcom.

Important point: You tell the compiler the directory path that consists
the package and not the package itself. It is the most mistaken thing people
assume.

For this example you have to tell the -cp as . means current directory,
current directory is test and that consists the xcom package. And to refer
to the B.java you say xcom/B.java because it gives the complete path
to the B.java from test directory. Compiler can find class A also in this
way because of (-classpath .).

Package is nothing but a simple directory.

Thanks,
[ July 05, 2007: Message edited by: Chandra Bhatt ]
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra for reply.
But in this example, if test is current directory, how can compiler find A.class file from test directory as it is in a xcom package.Because(. xcom/B.java) is for finding B.java file not for A.class.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Because class A and class B are in the same package xcom as class A is written
as



xcom is in the test directory, and you say -classpath . means find the class
on the current directory. xcom.A and xcom.B are the full name of the class
files.

Thanks,
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So "B" is also a correct answer. Because in B, current directory is xcom & (.B.java) says to find a class from (xcom.B.java). And A.class is also in a xcom so code will compile.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dolly,

It seems you didn't take the vital part of the issue: OK!
See what Ivor Horton says in his book Beginning Java 2:


The path to the package directory is the path to the directory that contains
the package directory, and therefore does not include the package directory
itself. for example, if you have stored the source files for classes that
are in the Geometry package in the directroy with the path C:\Beg Java Stuff\Geometry, then the path to the Geometry directory is C:\Beg Java
Stuff. Many beginners mistakenly specify the path as C:\Beg Java
Stuff\Geometry, in which case the package will not be found.



I have made the statement in bold face, where you miss the point to
understand.

Thanks,
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra for detail explanation.
I did it practically & I am damn sure, answer "B" is correct. They are wrong.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Dolly,


It seems that test directory is on your classpath, so that compiler finds
class A. You can compiler class B being inside xcom directory; NO problem!
suppose you want to compiler class B from being C: drive you do like:

C:\>javac -classpath "c:\test" c:\test\xcom\B.java

As explained in the answer too, that you need to tell the compiler where
to find class A telling it through -classpath.

Note: When you use -classpath or -cp, it overwrites the existing
classpath settings.

Be sure you understand my previous post.

Thanks,
[ July 05, 2007: Message edited by: Chandra Bhatt ]
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic