Classes, Objects, Bears Oh my!

Kelsey H.
3 min readDec 4, 2023

I’ll be using the code snippet below to go into more detail about some of the things I learned about classes and objects in Java.

public class Zoo{
public String name;
public int numOfAnimals;
public boolean openToVisit;

public Zoo( String name, int numOfAnimals, boolean openToVisit ){
this.name = name;
this.numOfAnimals = numOfAnimals;
this.openToVisit = true;
}

public static main void(String[] args){
Zoo myZoo = new Zoo("Bronx", 200, true);
Zoo anotherZoo = new Zoo("Central Park", 50, false);
}
}

In Java, you define a class by using the keywords public class. Inside your class, you can initialize instance variables, define constructors, and also methods. Instance variables have a class level scope, able to be accessed throughout any of the methods or constructors defined within your class.

public String name; 
public int numOfAnimals;
public boolean openToVisit;

Another way to think about classes is to think of them as factories for creating objects. They are blueprints for you to model any sort of real-world data.

Constructors define the attributes of your object, an instance of your class.

public Zoo( String name, int numOfAnimals, boolean openToVisit ){
this.name = name;
this.numOfAnimals = numOfAnimals;
this.openToVisit = true;
}

The Zoo object has 3 attributes: name, number of Animals, and whether it is open to visit. The this keyword refers to the current object and assigns it to the value passed via the parameters defined by your constructor.

public static main void(String[] args){
Zoo myZoo = new Zoo("Bronx", 200, true);
Zoo anotherZoo = new Zoo("Central Park", 50, false);
}

In the main method, I defined 2 new Zoo objects by first stating the type “Zoo” and the object name “myZoo” and “anotherZoo”. The new keyword tells the computer, “I want to create a new object with these attributes”. Now if I define any methods to be used with my objects, I can use myZoo and anotherZoo to call them. Here’s an example:

public void greetGuest(){
System.out.println("Hi! Welcome to the " + myZoo.name + "zoo!");
}

public static main void(String[] args){
Zoo myZoo = new Zoo("Bronx", 200, true);
Zoo anotherZoo = new Zoo("Central Park", 50, false);

myZoo.greetGuest(); //prints "Hi! Welcome to the Bronx zoo!"
}

Classes allows for encapsulation — hiding variables from other classes, and allow access to those variables through methods. In other words, wrapping data and keeping it separate so that only the necessary data is accessible. Public and private are access modifiers, used to let the program know what can be accessed by all classes and what cannot be.

Maybe in our zoo example, we want the name to continue to be public, but we want the number of animals in the zoo to be private and only accessible via a getter method.

private int numOfAnimals; 

public int getNumOfAnimals(){
return numOfAnimals;
}

Variables and methods that are private are only accessible in the class they are defined in. This means if another class wants to access the numOfAnimals variable they would not be able to use myZoo.numOfAnimals. That’s where getter methods come in. Getter methods return values from an object.

Instead of using the keyword void, you would let the class know what type of value to expect back. The getNumOfAnimals() method returns an integer in the example. Using getter methods maintains the variable’s privacy but also allows for it to be accessed in other classes.

myZoo.getNumOfAnimals(); //returns the num of animals in myZoo

The concept of encapsulation is super interesting to me, because I like the ability to bundle logic into categories. There are plenty of real-world applications of this where ensuring that data remains protected and separated is important.

I hope this article helps to expand a little more into classes and objects, providing more context onto the different parts of classes themselves.

Stay tuned for my next article about Strings (which are actually objects in Java) and the methods you can use with them!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Kelsey H.
Kelsey H.

Written by Kelsey H.

My three favorite things are coding, gaming, and caffeine.

No responses yet

Write a response