• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Package Members. Package naming. Question about example from jls

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic