O'Reilly logo

Java All-in-One For Dummies, 5th Edition by Doug Lowe

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 2

Making Your Own Classes

IN THIS CHAPTER

check Creating your own class

check Looking at the pieces of a class declaration

check Finding out about class fields

check Constructing constructors

check Adding methods to your classes

check Using the this keyword

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 are 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, I 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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required