Skip to content
Code To Live By » Java Tutorial » Classes in Java

Classes in Java

  • by

We have touched on classes in some of our other posts, now I think its time that we take a more in depth look at classes in Java.

Java is an “Object Oriented Programming” language. This means that nearly everything in Java relates to an “Object.” Every class in Java extends from the Object class.

Declaration

A class declaration contains many of the same elements as functions:

  • Access Modifiers (public, private, etc.)
  • Non-Access Modifiers (final, static, etc.)
  • Class Name

As an example, let’s say we wanted to make a class to represent Ice Cream as I try to ignore the chocolate and peanut butter gelato in my freezer right now. The class declaration for such a class may look something like this:

package code.to.live.by

public class IceCream {
   [...]
}

Our class is public meaning, again similar to Functions, that anyone may create instances of our “IceCream” class. We do not have any additional modifiers and our class name is simply “IceCream.”

Unlike many of our other of our other topics up to this point which have also utilized a main method, we will put our class in an entirely separate file dedicated to it. If you recall back to our Basic Program Structure post you may remember the concept of “packages,” we will place our new class in a code.to.live.by package.

Attributes

Attributes of a class are variables which belong to that class. Similar to Variables, we will need a data type and a name for our attribute. However, our attributes will also need an access modifier to specify who is allowed to access them.

In our “IceCream” we may have something like this:

public class IceCream {

    private String flavor;
    private int scoops;

    [...]
}

We have a variable for the flavor of our ice cream, which we will represent as a String, and as well as a variable for the number of scoops currently in our ice cream.

Constructor

A “Constructor” is how we can initialize new instances of our class. In Java, classes will by default have a zero parameter constructor when no other constructors are specified. For our IceCream class, that may look like this:

IceCream iceCream = new IceCream();

However, we can do other things in our constructor, such as initializing some of our attributes:

public IceCream(String flavorParameter, int scoopsParameter) {
	flavor = flavorParameter;
	scoops = scoopsParameter;
}

Now we don’t have to worry about trying to set our attributes later, we can know that they were set as part of the constructor. We could even have multiple constructors for our class. Let’s say if a number of scoops wasn’t specified, we would want to default to zero. That may look something like this:

public IceCream(String flavorParameter) {
	flavor = flavorParameter;
	scoops = 0;
}

Our IceCream class could then be initialized through either constructor.

IceCream mooseTracks = new IceCream("Moose Tracks", 3);
IceCream strawberry = new IceCream("Strawberry");

Methods

Methods can also be associated with a class, and they will share many of the same qualities we discussed earlier on our dedicated page for Functions.

You may remember earlier we set our attributes to private. If we need a way to access them we can do so by calling a public Method we create.

public String getFlavor() {
	return flavor;
}
	
public int getScoops() {
	return scoops;
}

Now we have two public methods which we can use to get our attributes off of IceCream objects. This is part of larger concept called encapsulation. Callers then don’t need to know the specifics of what attributes on our class, they only need to know how to get the information they need.

What if we needed some way to change our attributes? Maybe something like adding or removing scoops of Ice Cream? We could add some functions that do something like this:

public void addScoops(int numberofScoopsToAdd) {
	scoops += numberofScoopsToAdd;
}

public void eatScoop() {
	scoops--;
}

Now we could put some of this together to get something like this:

IceCream iceCream = new IceCream("Chocolate Marshmallow", 3);

while (iceCream.getScoops() > 0) {
	iceCream.eatScoop();
	System.out.println("Yum! I had a scoop of " + iceCream.getFlavor() + " ice cream.");
}

System.out.println("All out of " + iceCream.getFlavor() + " ice cream :(");

This would give you the following output:

Yum! I had a scoop of Chocolate Marshmallow ice cream.
Yum! I had a scoop of Chocolate Marshmallow ice cream.
Yum! I had a scoop of Chocolate Marshmallow ice cream.
All out of Chocolate Marshmallow ice cream :(

Now you have a good idea of how to create and use classes in Java!

Leave a Reply

Your email address will not be published. Required fields are marked *