Click this widget to change the font size.
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to Object-oriented Programming With Java (University at Buffalo 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 (put-your-username-here should be all lowercase with no space or apostrophes):;

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

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


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.


 * File:
 * 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.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 Code
I've coded, compiled, and run my first Java program!

Figure 3-2: Sample Java Program Output

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland