Guide to Object-oriented Programming With Java (Buffalo State College Version)


Chapter 3: Naming Packages and Classes

Naming Conventions

A Java package is a "container" for your Java classes. Your packages should be named as follows (your-last-name should be all lowercase with no space or apostrophes):

buffalostate.edu.sp18.assignments.your-last-name;
or
buffalostate.edu.sp18.exercises.your-last-name;

(depending on whether its an exercise or an assignment ;-))

So, the first line of every .java file should be:

package buffalostate.edu.sp18.assignments.your-last-name;
or
package buffalostate.edu.sp18.exercises.your-last-name;

Each Java program can contain only one (1) class method. The name of the class must begin with an Uppercase letter. The name of your .java file should be the same as the name of your class. For example, the file HelloWorld would have a public class HelloWorld { } statement.

A Sample java Program

Every Java program should start with the package line. Next you would have any import statements, the class line, any class variables, then class methods, and finally the main() method.

package buffalostate.edu.sp18.assignments.your-last-name;

/**
 * File: MyFirstApplication.java
 *
 * This is a sample Java Application
 * It prints a short statement to the standard out
 *
 * @author Your Name Here
 *
 */


import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;

public class MyFirstApplication {
 /**
  * The main() is the entry point for a Java program when executed
  *
  * public means it is publicly accessible (required)
  * static means there is no object defined yet at start of program
  * void means it does not return anything
  *
  * @param string array - command line arguments passed to program
  * @return none
  *
  */

  public static void main(String[] args) {
  // print to the console
    System.out.println("I've coded, compiled, and run my first Java program!.");
  }
}

Figure 3-1: Sample MyFirstApplication.java Code
Console
<terminated>
I've coded, compiled, and run my first Java program!

Figure 3-2: Sample Java Program Output
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2018 Jim Gerland