Chapter 10
Making Your Own Classes
IN THIS CHAPTER
Creating your own class
Looking at the pieces of a class declaration
Finding out about class fields
Constructing constructors
Okay, class, it’s time to learn how to create your own classes.
In this chapter, you discover the basics of creating classes in Java. All Java programs use or consist of classes, so you’ve already seen many examples of classes. You’ve seen class headers such as public class GuessingGame and static methods such as public static void main. Now, in this chapter, we show you how to create programs that have more than one class.
Declaring a Class
All classes must be defined by a class declaration — lines of code that provide the name for the class and the body of the class. Here’s the most basic form of a class declaration:
[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. After all, the main reason you write class declarations is so other classes can create objects ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access