Classes are the fundamental building blocks of a Java program. You can define an Employee class as follows:
class Employee {
int age;
double salary;
}
- By convention, class names capitalize the initial of each word.
- For example: Employee, Boss, DateUtility, PostOffice, RegularRateCalculator.
- This type of naming convention is known as Pascal naming convention.
- The other convention, the camel naming convention, capitalize the initial of each word, except the first word.
- Method and field names use the camel naming convention.
A Simple Class Definition
class Count { public static void main(String args[])
throws java.io.IOException
{ int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars."); }
}
In the Java language, all methods and variables must exist within a class. So, the first line of the character-counting application defines a class, Count, that defines the methods, variables, and any other classes needed to implement the character-counting application. Since this program is such a simple one, the Count class just defines one method named main().