Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Conventions for using packages?

Tags:

java

package

I am coming from a PHP background, and due to the lack of namespaces in the past I have used Zend Framework style "packages". For example, if I have an abstract class Player and children Player_Human, Player_DumbComputer, Player_Minimax, etc. I would put Player in the main directory and put it's children in a directory /Player/. It tried to do something similar in Java, but I got a name-clash -- I have both a package blah.Player and a class blah.Player. How do I avoid this? What is the common practice regarding this?

like image 430
moteutsch Avatar asked Sep 01 '26 15:09

moteutsch


2 Answers

From Code Conventions for the JavaTM Programming Language:

Package naming conventions:

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.

Class naming conventions:

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

like image 193
Eran Medan Avatar answered Sep 03 '26 04:09

Eran Medan


Java Package names are always in lower case while the Class names always start with an upper case letter and are camel case names (no underscores). So your structure should be:

- blah
|- Player.java
|- player
  |- HumanPlayer.java
  |- DumbComputerPlayer.java
  |- MinimaxPlayer.java
like image 26
Abhinav Sarkar Avatar answered Sep 03 '26 04:09

Abhinav Sarkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!