Lesson Weekend

As we previously discussed, almost everything in Java (besides primitives) is an object. Each of these objects are instances of a class. Many classes come built-in to Java and other programming languages by default, like String or Array. We've already worked with these built-in classes. However, the real power of programming comes when we can make custom objects from our own custom classes!

A class is like a blueprint that defines the behavior and attributes of a certain type of object. Objects may then be made with the "blueprint" of that class. While each individual object may have different attributes, they all have access to the same behaviours. You have worked with Objects in Intro, and in Java they are similar. Constructors, for example, work the same way.

For instance, "HEY THERE.", and "i love puppies!" are both String objects, and therefore instances of the String class. Although they contain distinguishably different words they both have access to the same behaviours such as the toUpperCase() and length() methods.

Declaring a Class

Let's walk through an example of creating a custom class together. Let's assume we've been hired to create a program to keep track of all available cars for a specific car dealership. Create a new project in IntelliJ called car-dealership.

First off, we know all our products will be of the same general type. They're all vehicles. We know we'll need a single Vehicle class. This class will reside in a file of the same name, Vehicle.java. Let's make a new directory to house our class file. Since it is a data model, we'll call it models. Right click in your src/main/java/ directory and create a new package called models. Then, right click in that, and create a new Java class called Vehicle.java. Make sure your class is defined as public.

You'll need the line package models; at the top of the class file to specify that this should be part of a package with the name models.

src/main/java/models/Vehicle.java
package models;

public class Vehicle {

}

Similar to how all words enclosed in quotes are called strings and all whole numbers are called integers, each object created from this class is called a Vehicle. When we use the keyword class, as we did above, we are declaring the class, just like we do when we declare functions.

Capitalization and Naming Rules

As seen in the example above, when declaring a class, use the class keyword first, followed by the name of the class you want to create. The name MUST use UpperCamelCase capitalization and contain no spaces or underscores. Vehicle.java is acceptable for a file that contains a class called Vehicle, but vehicle is not. Otherwise, you will receive build errors and your code will not compile. Then, just like declaring a function, a set of parentheses, and then curly braces should follow the name. The contents of the class will reside between these curly braces.

Class Properties

Now, objects do many things. But the simplest thing they do is store information. Next, we need to decide what details to include about each vehicle. What are general attributes a car dealership would record for each vehicle on its lot? If we take a brief glance at any website advertising cars for sale, we see that most cars for sale include the same key details. Let's include these same details for each vehicle in our program:

  • A year, so potential customers know how old the car is.
  • A brand (sometimes referred to as a "make") to record which company manufactured this vehicle.
  • A model to record the specific type of vehicle.
  • The number of miles the car already has on it.
  • The price of the specific vehicle.

Member Variables

So, we've decided what attributes our vehicles will have. We'll add these to our class:

src/main/java/models/Vehicle.java
package models;

public class Vehicle {
  public int year;
  public String brand;
  public String model;
  public int miles;
  public int price;
}

Each of these will be a property or attribute of our Vehicle class. Essentially, they'll be just like any other type of variable. While the specific values of these properties may differ from vehicle to vehicle (some may have 200,000 miles on them, whereas others may have 0, for instance), all Vehicle objects will be created using this same blueprint, containing these same details: Year, brand, model, miles and price.

Remember that the idea of a Vehicle as "a general transportation device with a price, and a certain amount of miles" is the class. Whereas the idea of a Vehicle as "A specific 1994 Ford Taurus with 150,000 miles on it" is an object that is an instance of the Vehicle class.

Additionally, we used the primitive int instead of its wrapper class Integer above, because we don't yet need to call any methods upon these attributes, so we can opt for the simpler primitive type.

Note on Terminology

As you're learning about Java, and consulting a variety of resources, you may hear the attributes or properties of a class referred to as fields, class variables, or member variables. Know that these all essentially refer to the same thing.

Access Level Modifiers

You may also notice that each property included in our class above is preceded by the word public. This is called an access level modifier, and it determines who may access this particular class. Declaring these properties as public simply means they're available to everyone. Other potential options include private and protected. We'll learn more about access level modifiers a little later on in the course. For now, we'll simply make everything public.

Whew, that was a lot of vocabulary! In the next lesson we'll learn how to create individual instances of our custom class, and in upcoming lessons we'll walk through retrieving details about these specific objects, integrating custom classes into Java Applications, and including custom class methods and constructors.

Terminology


  • Objects: Instances of classes that store information. What type of information depends on the class declaration.

  • Properties (also known as attributes): The types of information objects hold.

  • Class: A classification for an object, for example, you could have a class called Puppy and a particular object of that class could be a Chocolate Lab named Bruiser. And another object could be a Chihuahua named Todd. They're

  • Class Declaration: A blueprint explaining how each object of that class will behave, like a function declaration - it defines which properties the object will hold.

  • Member Variables: A variable that belongs (ie: "is a member of") a specific class. Refers to the attributes/properties we declare in our classes. In Java they're sometimes prefixed with an m in order for easy identification by other developers, but this is a matter of preference.

Examples


Below is a class declaration for a Vehicle class:

src/main/java/models/Vehicle.java
public class Vehicle {
  public int year; 
  public String brand;
  public String model;
  public int miles;
  public int price;
}
  • The Vehicle class acts as a "blueprint", determining what details (ie: attributes or properties) each object created using this class will contain.

  • Here, brand, model, miles, and price are all attributes, or properties of the Vehicle class.

  • Individual objects created from this class will each have values representing brand, model, miles, and price. But the value of each may differ. For instance, one object may be a 1994 Ford Taurus with 200,000 miles, whereas another may be a 2016 Subaru Legacy with 100 miles.

  • The word public that precedes each attribute is known as an access level modifier. It is required to include one, but until our projects increase in size, they don't affect us much. We'll make everything public until further notice, and explore this concept in greater detail later.