How to Add Buttons and Text to Your JavaFX Project
Java: Working with Operators
What Is an Initializer in Java?

How to Use Class Declarations in Java

In Java programming, a class is defined by a class declaration, which is a piece of code that 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.

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:

  • 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.

  • 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.

  • 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:

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

  • Methods: Method declarations define the methods of a class.

  • 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.

  • 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.

  • Other classes: A class can include another class, which is then called an inner class or a nested 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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Tips for Beginning Java Programmers: Variables and Recycling
How to Convert BigDecimals to Strings in Java
Bonus Chapters for Java All-in-One For Dummies, 4th Edition
How to Use an Enum Type in Java
Beginning Programming with Java For Dummies Cheat Sheet
Advertisement

Inside Dummies.com