Java Source File Example
The following example shows how to format a Java source file containing a single public class.
Pay attention to capitalization, comments and spaces.
package java.blah; import java.blah.blahdy.BlahBlah; /** * Class description goes here. * * @version 1.10 04 Oct 1996 * @author Firstname Lastname */ public class ClassName extends SomeClass { /* A class implementation comment can go here. */ /** classVar1 documentation comment */ public static int classVar1; /* * classVar2 documentation comment that happens to be * more than one line long */ private static Object classVar2; /** instanceVar2 documentation comment */ protected int instanceVar2; /** instanceVar3 documentation comment */ private Object[] instanceVar3; /** * ...constructor ClassName documentation comment... */ public ClassName() { // ...implementation goes here... } /** * ...method doSomethingElse documentation comment... * @param someParam description */ public void doSomethingElse(Object someParam) { // ...implementation goes here... // IF statements if (a == 2) { return TRUE; /* special case */ } else { return isprime(a); /* works only for odd a */ } // IF statements using alpha notation alpha = (aLongBooleanExpression) ? beta : gamma; // FOR loop for (int i = 0; i < maxLoops; i++) { statements; } // WHILE loop while (d++ = s++) { statements; } // SWITCH operator switch (variableName) { case ABC: statements; /* falls through */ case DEF: statements; break; case XYZ: statements; break; default: statements; break; } // TRY-CATCH operators try { statements; } catch (ExceptionClass e) { statements; } } }
Resource: Java Code Conventions