Journey to Java: Episode 6 “Intro to Object-Oriented Programming”

Java is among many programming languages that is an object-oriented programming language.

Object-oriented programming is about creating objects that contain both data and methods. It has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP helps to keep the Java code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time

What is a class and what is an object?


Classes are like templates to create objects. They pass down all variables and methods to the instances of the class. I like to imagine the class is a factory. Say you have a toy factory, the factory isn’t a toy, but it creates toy objects with a blueprint.


Objects are the “thing” being created by a class. It’s a way to represent real world things in code. There is no cat datatype but we can create a cat object that can walk, meow, eat, and have a color. Objects should have two traits, state and behavior. State is attributes of the object such as a “color”. Behavior would be something like “jump on the counter”

How to create a new class in Java

Create a file inside the src folder with a capital letter appended by .java. An example would be Inside that file create a new class.

Create a field or attribute

Classes allow us to create variables that can be used throughout the entire class. These are know as class variables or fields. When creating a field for a class you must also declare an access modifier similar to the class definitions. As a general rule when defining an access modifier in a field we want to use the access modifier private. Private adheres to encapsulation, which is one of the 4 pillars of OOP, hiding the field and methods from being accessed publicly while allowing access to only the class. To create a field we need an access modifier, a datatype, and a name:

Now that we have a blueprint set up to spit out cars from our car factory we can start to create instances of car objects.

Creating instances of objects

Inside our main method we can start to call to our Car class.

Methods can be preformed on instances of classes as it is a new datatype. we do not want to access these fields from other classes. Instead we should create class methods inside the class we want to work with.

Creating a class method

To adhere to encapsulation we want to make a public class method to be used through the program. To work on an instance of a class we can use the keyword this as a placeholder. In order to set the field of this we need to make it equal the parameter passed in to the class method. This is known as the setter method.

Now that we have a public way to operate and set a fields information we can go back to the main method.

From this point we could repeat this process to finish a full car object.

At this point in the process we are able to create a class, create an object that is an instance of that class, and access the fields of the object and update them using a setter. Now we need to be able to access those values using a getter method.

This has now created a method where we can return the value of the current instance we are working with. Now we can go to the main method and try to call this public method.

This is the end of this part of object oriented programming in Java. There will be multiple blogs on this amazing topic. In review we talked about:

  • What is OOP?
  • What is a class?
  • What is an object?
  • How to create a class.
  • How to create an object.
  • How to create fields.
  • How to create class methods.
  • How to set with setter methods.
  • How to get with getter methods.



Student at The Flatiron School in Washington DC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store