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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
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