Class Declaration

A class is defined by a class declaration, which follows this basic form:

[public] class ClassName {class-body}

The public keyword indicates that this class is available for use by other classes. Although it’s optional, you usually include it in your class declarations so that other classes can create objects from the class you’re defining.

CrossRef.eps For information about advanced forms of class declaration, see Abstract Class, Anonymous Class, extends Keyword, Final Class, and Inheritance.

The ClassName provides the name for the class. You can use any identifier you want to name a class, but the following three guidelines can simplify your life:

check.png Begin the class name with a capital letter. If the class name consists of more than one word, capitalize each word: for example, Ball, RetailCustomer, and GuessingGame.

check.png Whenever possible, use nouns for your class names. Classes create objects, and nouns are the words you use to identify objects. Thus, most class names should be nouns.

check.png Avoid using the name of a Java API class. No rule says that you absolutely have to, but if you create a class that has the same name as a Java API class, you have to use fully qualified names (such as java.util.Scanner) to tell your class apart from the API class with the same name.

The class body of a class is everything that goes within the braces at the end of the class declaration, which can contain the following elements:

check.png Fields: Variable declarations define the public or private fields of a class.

check.png Methods: Method declarations define the methods of a class.

check.png Constructors: A constructor is a block of code that’s similar to a method but is run to initialize an object when an instance is created. A constructor must have the same name as the class itself, and although it resembles a method, it doesn’t have a return type.

check.png Initializers: These stand-alone blocks of code are run only once, when the class is initialized. The two types are static initializers and instance initializers.

check.png Other classes: A class can include another class, which is then called an inner class or a nested class. For more information, see Inner Class.

A public class must be written in a source file that has the same name as the class, with the extension .java. A public class named Greeter, for example, must be placed in a file named Greeter.java.

You can’t place two public classes in the same file. For example, you can’t have a source file that looks like this:

public class Class1

{

// class body for Class1 goes here

}

public class Class2

{

// class body for Class2 goes here

}

The compiler will generate an error message indicating that Class2 is a public class and must be declared in a file named Class2.java. In other words, Class1 and Class2 should be defined in separate files.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset