aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Code from SCJP K&B Book won't compile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Code from SCJP K&B Book won Watch "Code from SCJP K&B Book won New topic
Author

Code from SCJP K&B Book won't compile

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I have this code in a file called Parent.java


and this code in a file called Child.java


I get an error when I compile Child.java.
package certification does not exist import cetfification.Parent

I checked the spelling and still do not know why this will not compile.


Visit my blog! http://jameshambrick.com
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy,

you have to compile both from the root directory that contains both package directories.


Bu.


all events occur in real time
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

both files are in c:\java. Package directory? they have to be in seperate folders from the same directory c:\java\certification and c:\java\other Ill try that

okay that did not work, I can also take your response as leave Child.java and Parent.java in c:\java and just create a certification folder as well as a other folder but that does not seem like it would work that way either.
[ August 03, 2007: Message edited by: James Hambrick ]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

after some fooling around I found that you have to have in this example a Other folder with the Child.java file and a certification folder inside the other folder. Inside the certification folder you need the Parent.java file.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Yes, when you declare a package, the file needs to be in a directory with the same name as the package.

The "certification" directory does not necessarily need to be in the "other" directory. It just needs to be somewhere that can be found so it can be imported.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

okay now that it works and I see that I cannot do this


because x is protected and I get a compile error as the book stated. It got me thinking. declaring a object of Parent inside the child class is composition(I think that's what it was called). But since its inherited you can declare an object of Child and it work fine. what would be the actual difference? I can still get to the value of x
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy ranchers,

James wrote:
I can still get to the value of x


Yes, but only from inside a Child object. You can't get the x from a Parent object outside its package.


And there is more: in the package other, you cannot access Child's x outside class Child.
If you try to compile this class:


So the protected fields in different packages are visible only through inheritance and only visible in classes (and subclasses of these) that inherit the class with the protected fields.
In the same package (other in the example) this fields are invisible in all classes that are no subclasses of the class (in the certif. package) with the protected fields (certification.Parents).


Complicated.

Yours,
Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code from SCJP K&B Book won't compile