Java
™
Coding Style Guide
Achut Reddy
Server Management Tools GroupSun Microsystems, Inc.
Created:January 27, 1998 Last modified:May 30, 2000
ABSTRACT
The importance and benefits of a consistent coding style are well known. This document describes a set of codingstandards and recommendations for programs written in the Java
™
language. It is intended for all Java software de-velopers. It contains no material proprietary to Sun, and may be freely distributed outside Sun as well.Feedback in the form of corrections or suggestions for improvement are welcomed. Comments may be sent to
achut@eng.sun.com
.
Table of Contents
Java Coding Style GuideiTable of Contents
.......................................................................................................................11.1 Background...................................................................................................................11.2 Acknowledgments.........................................................................................................1
........................................................................................................................12.1 Source file naming........................................................................................................22.2 Source file organization................................................................................................22.2.1 Copyright/ID block comment..............................................................................22.2.2
package
declaration.............................................................................................32.2.3
import
declarations.............................................................................................32.2.4
class/interface
declarations...........................................................................3
.........................................................................................................33.1 Package naming............................................................................................................43.2 Class/Interface naming..................................................................................................43.3 Field naming.................................................................................................................43.4 Method naming.............................................................................................................53.5 Local variable naming...................................................................................................53.6 Statement label naming.................................................................................................5
............................................................................................................64.1 Blank lines....................................................................................................................64.2 Blank spaces..................................................................................................................64.2.1 A single blank space (not tab) should be used:....................................................64.2.2 Blanks should
not
be used:..................................................................................64.3 Indentation....................................................................................................................74.4 Continuation lines.........................................................................................................7
..........................................................................................................................85.1 Documentation comments............................................................................................85.2 Block comments............................................................................................................95.3 Single-line comments....................................................................................................10
................................................................................................................................106.1 Class body organization................................................................................................116.1.1 Member access levels..........................................................................................116.1.2 Member documentation comments......................................................................116.1.3 Class and instance variable field declarations.....................................................116.1.4 Static initializer....................................................................................................126.1.5 Static member inner class declarations................................................................126.1.6 Static method declarations...................................................................................126.1.7 Instance initializer................................................................................................126.1.8 Constructor declarations......................................................................................126.1.9 Instance method declarations...............................................................................136.2 Method declarations......................................................................................................136.3 Local inner classes........................................................................................................14
Table of Contents
Java Coding Style GuideiiTable of Contents
6.4 Anonymous inner classes..............................................................................................146.5 Anonymous array expressions and array initializers....................................................15
...........................................................................................................................157.1 Interface body organization..........................................................................................16
..........................................................................................................................168.1 Simple statements.........................................................................................................168.1.1 Assignment and expression statements................................................................168.1.2 Local variable declarations..................................................................................168.1.3 Array declarations................................................................................................168.1.4
return
statement.................................................................................................178.2 Compound statements...................................................................................................178.2.1 Braces style..........................................................................................................178.2.2 Allowed exception to braces rule.........................................................................178.2.3
if
statement........................................................................................................188.2.4
for
statement......................................................................................................188.2.5
while
statement...................................................................................................188.2.6
do-while
statement.............................................................................................188.2.7
switch
statement................................................................................................188.2.8
try
statement.......................................................................................................198.2.9
synchronized
statement....................................................................................198.3 Labeled statements........................................................................................................19
................................................................................................................................20
Reward Your Curiosity
Everything you want to read.
Anytime. Anywhere. Any device.
No Commitment. Cancel anytime.