Leaning Java with TRiBot - Variables
Click Here for a List of All Tutorials
If anyone would like to provide any advice or feedback on the tutorial, please message me on Skype. If you have questions about the tutorial, please feel free to comment on the thread, and I will do my best to answer your questions!
As always, be sure that you read every line of this tutorial carefully. If you don't, you are going to find yourself very lost. When I tell you to stop and think about something, I'd definitely recommend that you listen!
By now you should have read, and hopefully loosely understand, my 3rd tutorial. If you haven't, I would highly recommend doing so before reading this.
If you recall, a Java program consists of classes. The classes create objects, which are then controlled by methods. If this doesn't make any sense to you, you should review my previous tutorials.
By now the above concepts should make at least some sense to you. If not, as always, feel free to message me on Skype (tacomanstan), or even better, post on the thread so others can benefit from your questions!
What is a Variable?
If you've taken any level of algebra at any time in your life, you probably already know what a variable is; x, y, z, a, b, c, etc. Those are some of the more commonly used variables in algebra, and they can be really annoying to keep track of.
Luckily for us, Java isn't algebra! Yay!
Variables in Java
Right now you're feeling pretty confident, and you should! You've learned a lot in these last 3 tutorials. However, if you are like me, at this point you have something nagging at the back of your head: "How do I actually tell the program what an object is?"
If you didn't understand what I meant by that question, that's okay. I'll explain to you the problem: When you want to make an object, you can visualize it in your head. You know what the object is, what it does, how it does it, what its purpose is, etc. How can you write code that will make the computer understand that object in the same way that you do?
"CAN YOU HEAR ME?"
Let's pretend for a moment that we've made a Car class (recall that classes are in charge of making objects). We then use our Car class to create 5 cars. Each of those cars is called an instance.
A good way to understand the concept of an instance is to look at World of Warcraft. In World of Warcraft, many dungeons are called "instance dungeons", or just "instances". When you enter these dungeons, you are entering a new instance of the dungeon. The only people in that dungeon are you and your party members.
Let's look at a RuneScape related example: Worlds. Each world is an instance of RuneScape. You know how that works. You hop to a different world and almost everything is the same as the world that you just hopped from. Almost. There are obviously going to be differences between each world (number of players, for example), and those differences are what make understanding instances so important.
Now let's go back to our car example. The most important thing to understand is that even though each car instance was created by the same class, each car might (and probably will) be completely different.
Just like how each world in RuneScape has different things going on, the same applies to our cars: One car might be yellow, one might be blue. One might have a Michelin tires, one might have Good Year tires. One might be longer than the rest. One might 2 doors, another might have 4. The list goes on and on!
Objects with Variables
In Java, a variable that is part of an object is called an instance variable. Instance variables define the properties of an object. Just like how methods are in charge of doing something with an object, instance variables are in charge of defining what an object is.
Think about a car. Think about all of the different parts of a car. Think about the characteristics of a car. You can also look at the picture above for some inspiration.
Here's what I came up with as some examples:
These are just a couple of things that make up a car. A car is incredibly complex, so there are many, many other things that you could have come up with.
That list, as you probably guessed, is a list of instance variables.
Instance variables can be anything that defines the object that they are part of. Notice how "color" and "wheels" are both on the list? While at first it might seem like those are completely different things, they really aren't. Think about it; a car has wheels, and a car has a color. One of those characteristics is more detailed than the other, but they are both characteristics of a car.
Here you can see a modified Car class from my second tutorial:
Variables in Methods
Instance variables aren't the only types of variables in Java. Sometimes you need to use a variable in the same way that you would in algebra. For example, let's say you have a class called Rectangle. We have made 2 instance variables: width and height (remember these variables help define the rectangle). We have also made a method: getArea().
If you haven't already, try and think for a moment as to why I chose not to make an instance variable for the area of the rectangle.
Actually think. Are you thinking? Stop reading. Stop it. Think!
Alright well if you've made it this far you've either already thought, or you're just ignoring me. That's okay, let's just move on (you didn't hurt my feelings or anything).
Hopefully that gave you enough time to at least try to think that through. If you couldn't think of anything, that's completely okay; most people couldn't think of anything either.
As I'm sure you guessed, area is another variable!
But wait, hold on a minute... It's not where width and height are. That's because area isn't an instance variable! Instead of being created as an instance variable, area was created as what is called a local variable inside of the getArea method. Local variables get their name because they are created inside of a specific method. A local variable is local to a certain method.
So you're probably noticing how I still haven't answered the most important question: "Why isn't area an instance variable?"
Look at the width and the height of a rectangle. Those are pretty obvious characteristics, right? In fact, they are the only two characteristics of a rectangle.
You're probably thinking "What...? Then what is the area if it's not a characteristic?" To answer that, think about what you need to make a rectangle. All you need the width and the height. That's it. One you have the width and height, the width and height are then used to calculate the area of the rectangle. This makes the area of the rectangle a calculation instead of an instance variable.
Useful Tip: If you are ever finding yourself wondering if something should be an instance variable or a calculation, think about it this way: If you set all of the instance variables in a class to a random value, would the resulting object still make sense? If the answer to that question is no, at least one of the instance variables you've chosen should be changed to be a calculation.
For example, let's make area an instance variable, and try with a rectangle. I'll set each to a random value between 1 and 20. Here's what I got:
width = 5
height = 17
area = 2
You should be able to see pretty quickly what the problem with that is. The area of a rectangle with a width of 5 and a height of 17 can never be 2. Making area an instance variable caused the rectangle to fail the "random value" test, so you know that area must be a calculation.
This is the end of the fourth installment of my Learning Java with TRiBot tutorial series. This session was all about learning what variables are, and the basics of how they are used in a program. At this point, we have gone through all of the aspects of a basic program. Congratulations!
This is the last tutorial before you get to start writing code, and write your first program!
If there is anything listed above that you would like a graphical representation of so you can visualize it better, please let me know and I will do my best to include it.
If there is anything you don't understand, please let me know! If you didn't understand it, chances are someone else didn't either.