The complete beginner’s guide to create a class in Java – J022

by Jun 19, 2015

DeegeU Java Course

“The complete beginner’s guide to create a class in Java” video is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.


Did “The complete beginner’s guide to create a class in Java” make sense? Test your knowledge with the quiz for this video.

Try at home

  1. Try thinking of different things to model as a class.   Answer who, what, where, why, when and how.
  2. Try creating errors in your program and compiling it

Transcript – The complete beginner’s guide to create a class in Java

Java provides you with a wide variety of primitives for your applications. But look around you. You see people, cars, houses, books. You do not see too many numbers or characters walking around. We need some way to represent real world objects or concepts. In Java, you can create your own types to model any concept your program needs. These models are called classes. In this lesson I’m going to teach you how to create a class in Java.

Before we start into how to create a Java class, I have a question for you my audience. I’m curious what videos you’re watching. Are they all programming videos? Gaming videos? What are your favorite videos to watch? Let me know by leaving them in the comments below.

We’re going to look at what problem classes solve. Then we’ll talk about what is a class, and what makes a good class in a Java program. We’ll finish up with the syntax for creating a class in Java.

What problem are we trying to solve with classes. With Java primitives, we have characters, booleans, integers, and floating point numbers.

Java create class - Free Java Course Online - DeegeU

Java Classes are blueprints for creating Java types

We need a duck. We could in theory stamp every duck in existence with an integer, but then how would we know that duck #2023 is a female Mandarin duck in India? There are all kinds of schemes we could cook up to store ducks in a number, but eventually they’re going to fall apart.

We saw a bit of this problem when we talked about arrays. With arrays, we avoided storing temperature ranges in 36 variables by using three arrays. Arrays are one type of class in Java. The purpose of an array class is to store a list of ordered values. What I didn’t show you was that arrays have behavior, and in Java arrays are a special type of class. We’ll cover that soon.

A Java class is an abstraction we use to represent a real world object or concept. When you’re thinking of things you might want to model as a class, think of nouns. Your attributes are adjectives, and your behavior are verbs. This is a bit simplistic, but it’s a good place to start. For your class you need to answer who, what, where, why, when and how. Who is calling the class. What does the class do. Where in your application does it do what it needs to. Why is this class handling the task? And finally when does it do it’s supposed to do. And how? That is your code.

Classes act as blueprints for our models in Java. The blueprint is used to create a class in Java. In the blueprint you specify all the attributes and behavior your real world object needs to be created. In creating a duck, we specify attributes like species name, weight, age, sex, and color. The behavior would be things like swim, eat, and quack.

These birds could be represented with a single class. These are all examples from the kind of class, “bird”. To say it another way, the class describes the kind of object we are talking about. The class we’d define would contain attributes that are common to each of these birds.

The best classes model single purpose concepts. That means it has one job. The reason you want to do this is, if you create a class that does 100 things, you’ll need to change it often. If it does one thing, you’re less likely to need to change the class. Every time you make a change to a class, you’re introducing the possibility of breaking the class. Breaking working code is bad.

Here’s a good example. A security class to authenticate and authorize users. It’s one task is to handle security in your entire application. This way if your class for application security changes, you’re changing this one class. It also makes it easy to replace, if you need to use a new class for security. On the other hand, if you need change how you render graphics in your application, you don’t want to touch the security code. 3 in the morning phone calls, because your users can’t log into your application is no fun.

What makes a good Java class - Free Java Course Online - DeegeU

What makes a good Java class ?

When you’re thinking of what will make a good class, try to keep your model simple. I had a professor who always said, “horses aren’t stupid. They are as smart as they need to be to be a horse.” Your classes should be the same way. Just focus on what they need to accomplish, and nothing more. If you just need a car, don’t bother trying to model the engine inside the car if you’ll never use it.

That doesn’t mean your class can’t be responsible for the behavior of other classes. For example a you might have a class representing a glowing power gem in a game. That gem could be in a box. So the magic gem is one class, and the box is second class. A class can be an attribute for another class.

Let’s try creating a class in Java. We said we needed a duck, so let’s create a Java class that represents a duck. We’re not going to define attributes or behavior yet. We’ll look at that in depth in another lesson. For now, I just want to show you the basics for creating a new type in Java.

When you create a class in Java, you’re creating a new file. Every class that is available to your application must be in it’s own file. The file has the same name as your class. If your class is called Duck, then it exists in a file called That’s why I said it was important that the name match the file when we worked on the HelloWorld application in the second lesson.

* Screencast create a Java class

OK. We now know what makes a good class. We also know how to create a class in Java. If you have any questions about how to create a class in Java, please leave them in the comments below.

In the next video, we’ll look at using our Java classes in our applications by turning our type blueprints into actual values.

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Get the code

The complete video lesson list for this free online programming course can be found on the course syllabus page.

The source code for “How to override a method in Java” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!



DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This