Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package Members. Package naming. Question about example from jls

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some examples:
Because the package java.awt has a subpackage image, it cannot (and does not) contain a declaration of a class or interface type named image.
from jls 7 7.1. Package Members

i see upward A package may not contain two members of the same name, or a compile-time error results. that as i supposed in one package must only one class or interface with one name.
What meaning of exemple? Because the package java.awt has a subpackage image, it cannot (and does not) contain a declaration of a class or interface type named image.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergej Smoljanov wrote:What meaning of exemple? Because the package java.awt has a subpackage image, it cannot (and does not) contain a declaration of a class or interface type named image.

That means you can't give a class/interface the same name as one the subpackages of the package the class/interface is in (just like you can't have a class and interface with exactly the same name in the same package). Normally this could/should never happen, unless you deliberately violate the naming conventions (packages should be all lower case, class/interface names should be CamelCase). If you try to do so, you'll get a compiler error. And that makes sense!

A little code snippet to illustrate:

Now I can create without any problem a package com.oracle.image (so no compiler error, only a warning on class image in package com.oracle: "The type image collides with a package").

But then I (try to) create the following class:

Now I get a compiler error ("The package com.oracle.image collides with a type"), because the package name is exactly the same as class image in package com.oracle.

Hope it helps!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic