You should know what a Java object is before creating Java objects. If not, you should take a look at Intro to Java Objects. Without this basic understanding of objects you're going to have a difficult time figuring out what objects are good for, as well as have a hard time figuring out how to use them.
So far I've demonstrated objects using previous programs. However, not all objects are going to need that main method we're so used to using.
I have said that Java objects are a lot like objects in real life, so let's create a real life object from scratch. Let's create a dog.
To begin creating Java objects, we'll create a new Java class named Dog. Leave it as such:
Next, we need to think about what a dog is. A dog is just a set of characteristics that makes it different from another object. Let's make a list of what makes a dog.
This is a very small list, but one we can work with for now. These will be the traits that will we give to our example dog object. When creating Java objects, lists like this can help keep track of what you need to code!
We're need to create variables to store these traits. These variables need to be defined for the entire object, so we are not going to create them inside of any methods.
Create these variables as shown:
First of all, these variables were created by putting the word private in front of them. This means that these variables can only be used inside of the dog object. Simple as that. Then I create four variables, one to store the number of legs, one to tell me if the dog has a tail, another to tell me what type of hair the dog has, and the last variable tells me the sound this dog makes.
All objects have a default constructor. This is true of even the other programs that we've made throughout these tutorials. Here is the constructor for our object Dog.
You'll notice that it looks almost like a method. The difference is, the default constructor is public without the keyword static, and has no return type. Meaning, it does not have void, or int, or anything else. Also, the name of the constructor is the same as the name of the object. ALL CONSTRUCTORS MUST BE THE NAME OF THE OBJECT. There are no exceptions to this rule.
What makes this the default constructor is that it has no parameters inside of the parenthesis. The parenthesis have nothing in them.
So why have you never seen this in any of our programs before? Didn't I just say that all classes have a default constructor?
The default constructor is created automatically when creating Java objects. You do not need to write it if you're not going to use it for anything. In general though, it is a good habit to create a default constructor for Java classes that will be used as objects, and I will be doing that in my examples from now on.
When creating Java objects, it's a good idea to give other objects access to some information about your object. In order to let other objects and classes have access to Dog's variables, we need to create getters. In order for outside objects and classes to change the variables, we need to create setters. Here's what I mean in a real-world analogy.
Let's say there's a painting. The painting will have characteristics like how tall it is, what colors are used, etc. Who cares about these properties though unless you can see them? In order to allow the outside world to see these properties, there needs to be a way to get these properties to others. That's exactly what getters do. They provide the eyes that others use to see the object's variables.
Now, let's say you created the painting, but you want to change it a little bit. You need to be able to change the properties of the painting. In other words, you want to change its variables. Setters give you access to variables in case you want to modify the data inside. A painter might do this with a brush; think of the painter's brush as the setter for changing a property of the painting, in this case the color.
Let's create getters and setters for the legs variable:
Ok, so you see the method getLegs()? That's the getter. Notice how I named it getLegs(). It does not have to be named this way, but it is an industry standard to name your getter method with a get in front of it, and then the variable name. It's return type is int because legs is an int, and that's all. Notice how it is NOT static. Getters and setters should not be static although to keep this tutorial short I will not go into why that is.
The setter method is setLegs(int x). Notice how I have one int parameter called x? Remember to set a variable you need to give it the new data, so that is what's being done. The return type is void because you're not returning anything, you're just setting data. Again, this method is not static.
Note: See how I pass in a variable called x as a parameter? I could also easily pass in a variable called legs. But wait a minute...
How can that possibly work? It can't. Java can't tell the difference between the legs parameter and the legs variable in Dog. However, I claim that this can still be done. The keyword to use here is this.
There. Now Java knows that this.legs refers to the variable declared at the top and not the parameter.
Continue to create getters and setters for each variable until your program looks like this:
Creating Java objects is not hard. Now let's learn how to use them. With the basic properties of a Dog defined, we may now create Dogs. To do this, we need to create another class. Create a new Java file called DogCreator and give it a main method. We'll be needing the main method so we can run the program.
Create a Dog object the way you would any other object:
Now, we need to give our new Dog some properties. Remember we need to say how many legs it has, whether it has a tail or not, and what kind of hair has the Dog. To do this, all you have to do is call those setter methods, giving them the appropriate information you want.
In my example, I said that the dog should have 4 legs, that it's tail property should be true (meaning it has a tail), and that its hair is furry.
Now that you have set the variables, we can easily get that data back and make use of the information. I'll just write some code that displays the information for this dog:
This will print out all the information on our dog object. Notice how I made the method calls INSIDE the println. This is allowed because the methods return data that can be easily printed out. The primitive types (the basic types) as well as String are easily printed out, so it is ok to put these method calls inside of the println statement. If these methods did not return anything, or if they returned something that's not easily printed out, you will either get garbage as output or an error in Java telling you to fix the problem.
One benefit of creating Java objects is that you can create more than one object. You can create more Dog objects, each one being it's own separate entity. Just make sure to name each new Dog variable something different. For example, I could create a dog2, dog3, and dog4, and give them completely different properties.
I hope this has helped you in creating Java objects, as they are quite important for creating more robust programs that are easy to code and understand. Object oriented programming is the newest trend that seems unlikely to change in the near future. Getting a hang of these now will allow you to understand the rest of the Java language far more easily than those who get stuck, so be sure to get plenty of practice creating Java objects. As an exercise, you could try creating a bunch of household objects, and then create and display their properties in a class that creates all of those objects.
If you have any questions, comments, or concerns about creating Java objects, feel free to contact us.