Jump to content
TacoManStan

[Tutorial] Learning Java with TRiBot #4 - Variables

Recommended Posts

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!

Introduction

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.

Recap

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.

RNbbFKU.jpg

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?

Qczd7l5.jpg

"CAN YOU HEAR ME?"

Instances

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!

hkYksjb.jpg

 
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:

Steering wheel
Wheels
Color
Engine
Doors

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.

ZsFawDh.jpg

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:

CvQEe0F.png

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().

VeHqza1.png

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.

KQpMmo0.png

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.

Conclusion

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.

Edited by TacoManStan
  • Like 17

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Our picks

    • This release includes:

      Fix shift clicking option selecting


      Fix high paint delay settings saving


      Update prayer IDs for the quick select menu


      Remove RS3 support


      Fix hooks



      RS3 Support Removed

      The RS3 client hasn't been updated since our Old-School version of TRiBot was released, as many of you may have noticed. Keeping all of the RS3 code in the client made the client as a whole harder to maintain, larger, slower, and messier. As hardly anyone still uses the RS3 client, and since the RS3 API was hardly functioning, we made the decision to completely remove it from TRiBot.

      For the average user, this means that the client will be smaller, cleaner, and faster. Future updates will also take less work meaning there will be more frequent updates.

      If you were one of the few users still using the RS3 client, we apologize for the inconvenience. No future support for RS3 is planned. There are many other botting clients which has support for RS3, so we recommend finding an alternative if you wish to continue botting on RS3.
        • Thanks
        • Like
      • 21 replies
    • Please welcome our new developers, @JoeDezzy1, @erickho123, @Encoded, and @wastedbro.

      These members will be responsible for working on, maintaining, and improving TRiBot.

      This means that bug fixes and improvements will now come at a much faster pace! We're committed to providing users with the best botting experience possible!
        • Thanks
        • Like
      • 30 replies
    • This release includes:

      More 3rd party libraries for script writers to use


      Apache Commons Codec


      Apache Commons Collections


      Apache Commons Configuration


      Apache Commons IO


      Apache Commons Lang


      Apache Commons Math


      GSON


      Guava


      JFoenix




      Hint arrow API


      Game#getHintArrowX


      Game#getHintArrowY




      Fix player hooks including Player#getSkullIcon and Prayer#getPrayerIcon
        • Thanks
        • Like
      • 49 replies
    • This update includes:

      Fix broken hooks


      Fix login bot for the message "No reply from login server. Please wait 1 minute and try again."


      Fix bug relating to which bot tab is sent human input


      General#randomLong bug fix involving negative numbers


      Fix GE API



      Please note: There are still some issues with the login bot due to a change in the game mechanisms handling the login screen. We're working on a fix and will upload it when ready.
        • Thanks
        • Like
      • 37 replies
    • This release will:

      Fix key event handling issue


      Fix other event handling issue


      Fix RSServer hook


      Update world hopper to have it use OCR, thanks to Todd


      Use proper disposal of old Graphics objects


      Reformat code


      Rearrange code


      Organize code imports


      Apply around 8000 automated code refactorings


      Make preparations for Java 9


      Fix 11 various bugs


      Add more reliable debugging support


      Fix mouseEntered/Exited event dispatching bug


      Fix minimap walking bug where it opens the map


      Fix broken hooks for today's game update
        • Thanks
        • Like
      • 100 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×