Forums Register Login

Why is using the default package discouraged?

+Pie Number of slices to send: Send
Topic.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

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
+Pie Number of slices to send: Send
So what should you name the package? A similar name to the Project?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
And as always, There's A Tutorial For That™ -- Creating and Using Packages. The second page in that tutorial thread answers your recent question.
+Pie Number of slices to send: Send
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.
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1260 times.
Similar Threads
Unable to create managed bean. Hello world app.
why is defult package use discouraged in eclipse
Doubt in JSP standard tag
Default and Subpackage
Tomcat Welcome ROOT web app problems - only some content displaying
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:03:46.