Aqua Phoenix
     >>  Lectures >>  Java 1  
 

Navigator
   
 
       
   

1.5 Classes - Definition

Java code is embedded in Classes. A class is a basic unit of a Java program and denotes a kind of object. A Java program may consist of one class (e.g. HelloWorld) or many classes. The more complex a program becomes, the more classes it tends to have. Typically, there is one executable (runnable) class that starts an entire program.

While viewed far to simplistically, consider casting Microsoft Windows into the view of Java objects: The Desktop is an object (Desktop.class), the Control Panel is an object (ControlPanel.class), and so forth (MSWord.class, MSExcel.class, ...). Smaller parts of ControlPanel.class may include AddRemovePrograms.class, AddRemoveHardware.class, Printers.class, etc. Desktop.class might be set-up as a Runnable class, so that the Windows interface can be started.

Rules and Observations

  • A class is a definition. A class defines methods and global variables. It defines the behavior of an object that is instantiated from this class.
  • Java code must be embedded in a class. There is no such thing as code outside of a class definition. In general, this is a template for any class:

    public class CLASSNAME {

      [ IMPORTED LIBRARIES ]

      [ GLOBAL VARIABLES ]

      [ METHODS ]
    }
  • Usually, one class is defined in a .java source file. It is possible to define more than one class in a file, but it makes the Java source less intuitive.
  • The filename for a class MUST be the same as the class name (e.g. class HelloWorld is saved in file HelloWorld.java) This is how the JVM identifies classes, in order to instantiate them.
  • Capitalization matters. An object by the name of HelloWorld is different from an object by the name of Helloworld. The filename MUST follow the same capitalization.
  • White spaces are not allowed in class names. There can be no such class of name Hello World. This will NOT work.
  • The declaration of a class is preceded by the modifier public. By default, if a class is not declared as public, it is still public. There is no such thing as a private class, otherwise classes could not be instantiated from other classes.