How to name packages java

What package naming convention do you use for personal/hobby projects in Java?

So, my Wicket project would be in the package myprojects.learningwicket and unit tests would be in the package myprojects.learningwicket.tests (for example).

Common, get yourself a personal domain (firstname-lastname.net) and use it as a package name. The purpose of package is to be globally unique, so myprojects don’t really cut it.

Using an .onion domain is also an option (like onion.duskgytldkxiuqc6.packagename ). As long as the private key remains secret, you’re in control of the domain name. So it’s free to register (generate), and it’s permanent (unlike conventional domains). It conforms to the letter of Java convention, and it identifies you uniquely.

@GabrielBB, GitHub Pages allows you to host HTML and Jekyll sites at yourusername.github.io, and therefore subdomains of github.io are more «guaranteed» to correspond to GitHub users than subdomains of github.com (which might be used for internal GitHub projects at any time).

12 Answers 12

If you’re just doing personal projects where nobody else will use the code, then you can make up a package name that you like. Don’t make up something that starts with com. or net. or other top-level domain though, because that would imply that you own the domain name (ie. using com.john as your package name just because your name happens to be John is not a good idea).

Читайте также:  Api qiwi bills php

If you’re going to give the code to anybody else, you should use a globally unique package name, which according to Java conventions means you should register and use a domain name.

So If I want to address the package name as com.xyz should this package name be registered somewhere?? P.S. xyz is my client.

I wonder if I can use my github id for that and github domain? E.g. com.github.mygithubid.myproject ?

@KirillG. I would kind of recommend against it, since GitHub IDs can be changed at any time, any number of times. Granted, any domain can change, but not as easily or often (not normally anyway). Though I guess it’s OK for hobby projects.

I just use my initials: fg.nameofproject.etc

It reduces typing. It can be prefixed at any time with sf.net or com. or org. or com.google..

As the project is personal treat it special just like your freshly pressed personalized gift shirt — it will feel good.

@click according to the guidelines that would be bond.james._007 — doesn’t have the same ring to it. :-

Bah. Any self-respecting programmer would have his or her own domain name. This is clearly a trick question. Everyone has their own personal domain name! 🙂

Ok, in all seriousness, buying a custom domain name is probably the easiest option. For about $10 a year, you can find reputable providers to host the domain and forward email.

Good answer, but a starting programmer probably doesn’t want to buy a website just to follow some tutorial website or video on how to create a GUI with a close button.

pmurray_at_bigpond_dot_com.project.package 

I store most of my hobby projects in Google Code, so I just use the project site as the package name: com.googlecode.donkirkby.someproject .

You miss Google Code, @Joey? I’ve mostly used GitHub for the last several years, so I just had to move a few of my old projects over from Google Code before they shut down.

I have a fantasy company of my own — sometimes I use that

the tradition with sourceforge(as hibernate and other packages showed) is net.sf.*

so, depending on your mood, you can go with that.

@anjanb Well, I went to they’re website. They do pet food. I’m no expert, but most pet food companies don’t release many software libraries =)

I think you’ve got it worked out. The temptation to avoid here is to not bother with a package name at all. It’s easy to save a few keystrokes because «I’m just writing some test code.» But then the code gets good and useful and large, and then you realize you have a solid start for what may be a long-lived library or application. It may not be a library or an application that ever leaves your home network, but the point is, you’ve not thought ahead. That’s the Denmark ghost of computer science — always think ahead, if only a little bit.

The naming convention I use for my hobby code is very much like yours. I have a top-level directory named «futura» (long, boring reasons why that name came about) that all my code hangs off of. I do try and organize my code into package libraries, even if it might be a class or package I never use for another project. I place all applications (that is, anything that has a void main(String[] args) in the class) in the futura.app.* folder. I also try to mimic the standard Java library package names for my own code, although in a couple of cases I broke convention because of my own tastes (i.e. futura.inet for Internet, not merely socket, code, and futura.collections for non-util stuff.) To paraphrase David Mamet: Always be genericizing. Always be genericizing!

From the care you used to post the question, I suspect you agree with my last point as well: You don’t have to treat hobbyist hacking as an enterprise-level project, but if you bring some of that discipline to the home game, the hobby is all the more rewarding.

Источник

Naming a Package

With programmers worldwide writing classes and interfaces using the Java programming language, it is likely that many programmers will use the same name for different types. In fact, the previous example does just that: It defines a Rectangle class when there is already a Rectangle class in the java.awt package. Still, the compiler allows both classes to have the same name if they are in different packages. The fully qualified name of each Rectangle class includes the package name. That is, the fully qualified name of the Rectangle class in the graphics package is graphics.Rectangle , and the fully qualified name of the Rectangle class in the java.awt package is java.awt.Rectangle .

This works well unless two independent programmers use the same name for their packages. What prevents this problem? Convention.

Naming Conventions

Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com .

Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, com.example.region.mypackage ).

Packages in the Java language itself begin with java. or javax.

In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as «int». In this event, the suggested convention is to add an underscore. For example:

Legalizing Package Names

Domain Name Package Name Prefix
hyphenated-name.example.org org.example.hyphenated_name
example.int int_.example
123name.example.com com.example._123name

Источник

What is the convention for word separator in Java package names?

Note that all this is just to ensure uniqueness. Only thing actually enforced is to stay out of the java.* space.

6 Answers 6

All three are not the conventions.

The package names do not follow camel casing or underscores or hyphens package naming convention.

Also, Google Java Style Guide specifies exactly the same (i.e. com.stackoverflow.mypackage ) convention:

5.2.1 Package names

Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example, com.example.deepspace , not com.example.deepSpace or com.example.deep_space .

— Google Java Style Guide: 5.2 Rules by identifier type: 5.2.1 Package names.

I partially agree — they are not ‘wrong’ according to the java naming conventions but they shouldn’t be used in my opinion. (java.sun.com/docs/codeconv/html/CodeConventions.doc8.html)

@JoseGómez «The prefix«. So imho this does not exclude all other words comprising a package name from being CamelCase or snake_case

I have been following this convention. However, the readability really isn’t very good. What is the reason behind all lower cases?

Using underscore is suggested in the Java package naming convention page, linked in the answer. Eg: «org.example.hyphenated_name»

@Haomin : the reason is that packages use the underlying file system, which might be case-sensitive or case-unsensitive. In the case of multi-developers, multi-platform project, it might lead to problems for folder names.

Here’s what the official naming conventions document prescribes:

Packages

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com , edu , gov , mil , net , org , or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization’s own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

Examples

References

Note that in particular, anything following the top-level domain prefix isn’t specified by the above document. The JLS also agrees with this by giving the following examples:

  • com.sun.sunsoft.DOE
  • gov.whitehouse.socks.mousefinder
  • com.JavaSoft.jag.Oak
  • org.npr.pledge.driver
  • uk.ac.city.rugby.game

The following excerpt is also relevant:

  • If the domain name contains a hyphen, or any other special character not allowed in an identifier, convert it into an underscore.
  • If any of the resulting package name components are keywords then append underscore to them.
  • If any of the resulting package name components start with a digit, or any other character that is not allowed as an initial character of an identifier, have an underscore prefixed to the component.

References

Источник

Оцените статью