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

Why is using the default package discouraged?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Topic.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Classes which are in packages can't import classes which aren't in packages (which is what it means to be in "the default package"). That's just how the language turns out to work. You can't write "import Example", in other words.

And since any non-trivial application is usually divided into packages, classes in the default package aren't useful there. So if you're writing a quick and dirty program to do something, then putting it in the default package is okay. But if it's for real work, then it probably needs to be in a package.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A better question would be "why is packages encouraged"? Packages are name spaces. It allows everything in the package to have some sort of a group name -- in addition of their names. This means that many teams can develop independently of each other, and not be too concerned about naming conflicts.


If all you intend to do is develop small programs, and not really work with anyone else, then I guess it should be fine. However, it is very easy to use packages, so why should you avoid it?

Henry
 
J. Chris Miller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what should you name the package? A similar name to the Project?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a "real project", it would be the company website name in reverse and then the project or anything after it. So com.coderanch.jforum.

For my test code, I often pick jb. as the package prefix.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And as always, There's A Tutorial For That™ -- Creating and Using Packages. The second page in that tutorial thread answers your recent question.
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think most developers use packages to organize their code structure better, e.g. separating code in different layers like model, DAO, bussiness, services, etc.
Also sometimes we separate code by functionality e.g. users, admin, books, sales, products, etc.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic