Institute of Computer Science
  1. Courses
  2. 2019/20 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2019/20 spring

  • Home
  • Materials
  • Grading
  • Java Glossary
  • Cheat sheet (S1-S6)
  • Source Example
  • Links

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

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment