Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code from SCJP K&B Book won't compile

 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

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


Bu.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic