The Java Class

Java is an object-oriented programming language. As a Java developer, your primary goal is to describe classes and objects. A class is a kind of category, like the category of all customers, the category of all accounts, the category of all geometric shapes, or, less concretely, the category of all MyFirstJavaClass elements, as shown in this listing.

package org.allyourcode.myfirstproject;
public class MyFirstJavaClass {
  /**
   * @param args
   */
  public static void main(String[] args) {
    javax.swing.JOptionPane.showMessageDialog
                                   (null, "Hello");
  }
}

Just as the listing contains the words class MyFirstJavaClass, another piece of code to describe accounts might contain the words class Account. The class Account code would describe what it means to be (for example) one of several million bank accounts.

You may know what js meant by the phrases “the category of all customers” and “the category of all geometric shapes,” but you may wonder what “the category of all MyFirstJavaClass things” means or in what sense a computer program (such as the program in the listing) is a category.

Here’s one answer (which is somewhat evasive): A Java program gets to be a “class” for esoteric, technical reasons and not because thinking of a Java program as a category always makes perfect sense.

Except for the first line, the entire program in the listing is a class. When you create a program like this one, you get to make up a name for your new class. In this listing, the name is MyFirstJavaClass. That’s why the code starts with class MyFirstJavaClass, as shown in the figure.

image0.jpg

The code inside the larger box in the figure is, to be painfully correct, the declaration of a class. (This code is a class declaration.) It is slightly imprecise to say (as it does in the figure) that this code is a class. In reality, this code describes a class.

The declaration of a class has two parts: The first part is the header, and the rest — the part surrounded by curly braces, or {} —is the class body, as shown in this figure.

image1.jpg

The word class is a Java keyword. No matter who writes a Java program, class is always used in the same way. On the other hand, MyFirstJavaClass in the listing is an identifier — a name for something (that is, a name that identifies something). The word MyFirstJavaClass is the name of a particular class — the class that you’re creating by writing this program.

In the listing, the words package, public, static, and void are also Java keywords. No matter who writes a Java program, package and class and the other keywords always have the same meaning.

<Warning>

tHE jAVA PROGRAMMING LANGUAGE IS cASe-sEnsITiVE. FOR EXAMPLE, iF YOU CHANGE A lowercase LETTER IN A WORD TO UPPERCASE OR CHANGE AN UPPERCASE WORD TO lowercase, YOU CHANGE THE WORD’S MEANING AND CAN EVEN MAKE THE WORD MEANINGLESS. iN THE FIRST LINE OF tHe lISTING, FOR EXAMPLE, IF YOU TRIED TO REPLACE class WITH Class, THE WHOLE PROGRAM WOULD STOP WORKING.

The same holds true, to some extent, for the name of a file containing a particular class. For example, the name of the class in the listing is MyFirstJavaClass, with 4 uppercase letters and 12 lowercase letters. So the code in the listing belongs in a file named MyFirstJavaClass.java, with exactly 4 uppercase letters and 12 lowercase letters in front of .java.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com