CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Java

Java Programming
Basic coding skills
Declare a main method

The syntax for declaring a main method
public static void main(String[] args)
{
   statements
}
How to declare a main method
  • A method is a block of code that performs a task.
  • Every Java application contains one main method. You can code the main method declaration as shown. The statements within the braces are run when the program is executed.
The main method of the InvoiceApp class
public class InvoiceApp
{
public static void main(String[] args)
{ // begin main method
System.out.println("Welcome to the Invoice Total Calculator");
} // end main method
}
Partial explanation of the terms in the main method declaration
  • The public keyword means that other classes can access the main method.
  • The static keyword means that the method can be called directly from the other classes without first creating an object.
  • The void keyword means that the method won't return any values.
  • The main identifier is the name of the method. When you code a method, always include parentheses after the name of the method.
  • The code in the parentheses lists the arguments that the method uses. Every main method receives an argument named args.
Previous | Code statements | Code comments | Code identifiers | Declare a class | Declare a main method | Next