Best Practices for Creating Java Object Classes

Jason Dinh
Strategio
Published in
4 min readApr 22, 2022

When I started coding, my biggest worry was whether or not my code met the expectations of employers and if it was readable to other developers. If you’re learning Java and having these concerns, you’ve come to the right post because, in this post, I’ll explain best practices for creating Java object classes to improve code readability.

This post is for beginners who are getting into Java.

Coding Style

First, your coding style will play a massive role in your code readability. In other words, how your code looks with the help of indentation is a fundamental skill for improving code readability. You can tell someone is a new coder if they don’t appear to have a coding style. There have been times when I’ve received code like this to review:

Bad Indentation

In this example, the code will run and work; however, the indentation is all over the place, and it’s probably the quickest way to make your team members hate working with your code.

Okay, I’ve shown you what hideous indentation looks like, so let me cleanse your eyes with better indentation.

Good Indentation

Notice how the structure of the Car class is a lot clearer. That’s because I followed the basic rule:

Substatements of a statement or declaration should be indented.

If you want a more in-depth explanation, I recommend checking out Google’s Java Style Guide.

Naming Conventions

Secondly, you should follow Oracle’s Naming Conventions. Following naming conventions allows your team to differentiate classes, attributes, and methods from each other.

Now that we’ve covered the essentials, we can start coding!

Let’s start with some pseudocode, so we can get an idea of what we need to do:

  1. Let’s declare our class! For this example, I’m going to be creating a Person class.

2. Now, let’s give our class some attributes. I will provide my Person(s) a name and an age.

I’ve declared the two attributes. However, we want to encapsulate these attributes so that the data can only be altered if we provide the methods to do so. We can encapsulate the attributes by adding the private keyword.

3. Next, we need to create a default constructor for our class so that it can be initialized without any argument values.

4. Although it is good practice to have a default constructor, we need a constructor that allows us to pass through arguments to store data in our Person objects. We can do that by overloading our constructor with another constructor that accepts arguments.

Overloading is defining multiple methods with the same name with different parameters. You’re probably wondering, “How does Java know which method to use?” When we initialize our objects, Java picks the corresponding method depending on the arguments we provide.

The following code will use the new constructor we created since we provided a String and int corresponding to our new constructor:

However, the following code will use our default constructor since no arguments were provided:

5. Lastly, we need to create getters and setters methods for our class since our attributes are encapsulated and can not be accessed outside of our class without these methods.

Let me show you a quick tip on how to create getters and setters in IntelliJ without having to write it yourself manually.

  1. Right-Click a line within the class

2. Click Generate…

3. Click Getters and Setters

4. Highlight the attributes you would like to create getters and setters for and press OK.

Now a set of getters and setters should be generated inside of your class!

Congrats! We just created a Plain Old Java Object (POJO) that is readable and follows enterprise standards. Now you can create object classes with confidence! 🎉

If you’d like to learn more, join me on my journey, where I’ll share my experience and knowledge by leaving a follow!

--

--

Jason Dinh
Strategio

Hey there! My name is Jason Dinh, and I’m a software developer based in Dallas, TX. Let’s get to know each other!