After reading the question Java packages com and org and answers, I am still curious:

  1. Is there some strict rule that I can use only com edu and org?
  2. And, is it bad idea to create package starting with something else than this?

Say, I am fro, Czech Republic and I own PavelJanicek company, can I create package which would be imported like

 import cz.paveljanicek.usable.awesomeutils;

edit: Since one of answers supposes it should be possible, can I apply it also to new domain TLDs?

example:

import berlin.berlincity.touristguide.api;
有帮助吗?

解决方案

A package name is defined by the language specification as a succession of valid identifiers, separated by ..

So the convention (for unicity purposes) is to use your domain name but any valid identifier can be used. This is a valid package name too:

é.è.û.¥

其他提示

You should check out this link: Java Packages and Java Package namings

You should also look at similar topic

At last a quote to add:

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).

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.

Short: Use whatever you like :)

yes, that's the way you have to do if you own paveljanicek. there are a lot of 'com' and 'org', but you can find many others; for example, logback logging library contains classes in package

ch.qos.logback....

You can use whatever you want just honoring the Java limitations for identifiers.

Said that, usually is safe using Java conventions, but killerapp.gui.Main is a valid class identifier

You missed a lot of the information given in the answer. Here once again a snipped of what Sun defined:

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.

Today we have even more top-level domains. The important part is to choose the domain you own in reverse order. I suggest you should read the answer once again, slowly. The goal is to avoid naming conflicts by choosing unique namespaces. And because domain names are already owned by a single company / person, they are good candidates to choose.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top