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
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 class in Java,
I have a question for you my audience. I’m curious what videos are you’re watching.
Besides this one. 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.
We need a duck. We could in theory stamp
every duck in existence, 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 will 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 data structure 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 very 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. 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 the 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 its own file.
The file has the same name as your class.
If your class is called "Duck", then it exists in a file called
"Duck.java". That's why I said it was important
that the name matched file we worked on in the Hello World application.
Okay, like I said earlier, to create a new class in Java
you're creating a file with the class name as the
name of the file. Now you can create it using your favorite text editor, but
I'm going to go ahead and do this in NetBeans. So hit File
| New File. For the category we want Java.
For the file type, we want Java class. Next
we'll call it Duck. We'll leave the
package empty for now. I'll cover packages in a later lesson.
And we hit finish. And we've created our class called
Duck. It starts with public
class and then the name of our
class which is this case is called Duck. Its
also in a file called Duck.java. And with that
we've created the Java class that we can use in our application.
Okay 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.
Till then. Hey thanks for watching the video.
There is a quick quiz for this on DeegeU.com if you'd like to gauge how much you
learned.
If you like the videos you are seeing, please let me know by liking the video
and hitting the subscribe button
for the DeegeU channel on
YouTube. I'd really appreciate that. If you have any concerns or questions, please leave them
in the comments below.
or on DeegeU.com. There's a poll on the front page
of DeegeU.com so you can let me know the topic covered
next. Thanks for watching, and see you in the next video!