Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
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 will:

      Fix LG for both OSBuddy and RuneLite


      Fix issue where the resizable client isn't able to be made smaller (Thanks @JoeDezzy1)


      Fix detection of the logout game tab when resizable mode and side panels are enabled (Thanks @JoeDezzy1)


      Add initial support for Sentry to allow us to identify and easily debug exceptions happening with all TRiBot users


      Add methods to determine if the bank is actually loaded, and not just the overarching interface (Thanks @wastedbro)



      Upcoming updates:

      Improved CLI support


      Full Sentry support


      Much more
        • Like
      • 51 replies
    • This release will:

      Fix NPE in Camera API (Thanks @wastedbro)


      Update deposit box interface ids (Thanks @Encoded)


      Add various bank methods (Thanks @wastedbro)


      Banking#getWithdrawXQuantity


      Banking#getDefaultWithdrawQuantity


      Banking#arePlaceholdersOn




      Fix resizeable minimap bug (Thanks @wastedbro)


      Remove Java 8 requirement


      Please note: TRiBot is not yet fully compatible with Java 10+




      Fix the break handler issues by ensuring the break handler thread never gets paused


      Fix broken settings hooks



      Upcoming updates:

      Improved CLI support


      Much more



      Note: If you are using LG, please restart both the RS client and TRiBot
        • Like
      • 68 replies
    • This release will:

      Add support for using custom F key bindings to switch between game tabs (Thanks @erickho123)


      Fix tab opening for "Skills" and "Kourend Tasks" (Thanks @erickho123)



      Note: If you are using LG, please restart both the RS client and TRiBot
        • Like
      • 34 replies
    • This release will:

      Fix an issue where breaks would stop firing


      Fix Combat#getWildernessLevel, use dynamic search for text and cache ID for later calls


      Fix an NPE in the Combat API


      Fix Mouse#leaveGame bug where the mouse wouldn't actually leave the game screen
        • Like
      • 21 replies
    • This release will:

      Add LG support for Runelite


      Fix NPCChat issues


      Fix a bug where the camera angle setter would just hold down a key for 5 seconds (the timeout)


      Slightly adjust the rotation via keys to be more accurate


      Add the ability for asynchronous camera movement via keys


      Make Camera rotation via mouse more fluid, with more antiban, and work much better in resizable mode


      Add a "Camera#setCamera" method, allowing the rotation and angle to be set in parallel


      Increase the likelihood of using the mouse for camera movements


      Add support for adjusting the camera to positionable entities (Positionable#adjustCameraTo)



      Upcoming updates:

      Improved CLI support


      Much more



      Note: If you are using LG, please restart both the RS client and TRiBot
        • Thanks
        • Like
      • 59 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×