CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Java

Java Programming
Basic coding skills
Declare a class

The syntax for declaring a class

public|private class ClassName
{
statements
}
Typical class declarations
public class InvoiceApp{}
public class ProductOrderApp{}
public class Product{}
How to declare a class
  • A Java application consists of one or more classes. For each class, you code a class declaration. Then, you write the code for the class within the braces of the declaration.
  • The public and private keywords are access modifiers that control what parts of the program can use the class.
  • The file name for a class is the class name with java as the extension.
The rules for naming a class
  • Start the name with a capital letter.
  • Use letters and digits only.
  • Follow the other rules for naming an identifier.
Naming recommendations for classes
  • Start every word within a class name with an initial cap.
  • Each class name should be a noun or a noun that's preceded by one or more adjectives.

A public class named InvoiceApp

public class InvoiceApp               // declare the class
{                                     // begin the class
   public static void main(String[] args)
   {
      System.out.println("Welcome to the Invoice Total Calculator");
   }
}                                     // end the class

The same class with different brace placement

public class InvoiceApp{              // declare and begin the class
   public static void main(String[] args){
      System.out.println("Welcome to the Invoice Total Calculator");
   }
}                                     // end the class

Previous | Code statements | Code comments | Code identifiers | Declare a class | Declare a main method | Next