Pregunta

I am creating a library (android). I keeping things as packages like com.domain.libname.datamodel; com.domain.libname.exceptions; etc.

I want the freedom of initializing some data model objects within my library which public can read, but public should not initialize those objects. Though 'package' access modifier does this within a package, i would like to do this across packages within the same project/library.

Is there a way? Or is it indicates i am not packaging things in the right manner?

¿Fue útil?

Solución

The package structure in Java is flat: If you want something to be visible outside of your package it will be visible to all packages; there is no way to restrict visibility to "subpackages" or to packages with a certain prefix.

My opinion is that a package should implement a feature, so that classes that have to cooperate can do so without having to expose needless cruft to the outside. This means that you wouldn't have "artificial" packages like "datamodel" or "exceptions"; instead the data models and exceptions would be in the package that actually needs them to implement a set of use cases. But I can see how in a large application or library that can become impractical.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top