Instance Classes

Now that we understand the basics of what classes and objects are in object-oriented programming, let’s look at how to create these items in Java.

Creating an Instance Class

Creating a class in Java is very similar to creating a method. The syntax is <access modifier> class <ClassName> {<body>}. We will use public for all our class-access modifiers. The class definition (body), like all Java code-bodies, is enclosed in {}.

Java requires just a single public-class in each file, with the filename matching the name of the class, followed by the .java file extension. By convention, class names in Java should be nouns, in mixed case (Pascal-case) with the first letter of each internal word capitalized1.

So, to create an empty class named Ingredient, we would place the following code in a file named Ingredient.java:

public class Ingredient {

}

As we’ve already learned, each class declaration in Python includes these parts:

  1. public - an access modifier enabling other parts of the code to “see” the class
  2. class - this keyword says that we are declaring a new class.
  3. Ingredient - this is an identifier that gives us the name of the class we are declaring.
  4. {}- an empty body that does nothing.

Following the declaration, we see a curly brace { marking the start of a new block, inside of which will be all of the fields and methods stored in this class. We should indent all items inside of this class, just like we do with other blocks in Java.

In order for Java to allow this code to compile, we must have a body. The { } can be empty but cannot be missing.