Aug 14, 2013 10:51 PM
(This post was last modified: Aug 14, 2013 10:52 PM by Pichu.)
![[Image: MCGSCNZ.gif]](https://i.imgur.com/MCGSCNZ.gif)
A Short Guide About Objects
Note: Not for complete beginners. This guide assumes prior knowledge on basic Java syntax and functiomsWhat is an object?
An object is an instance of a class. It can be compared to one of many buildings built using a single blueprint, the blueprint being a class and the buildings being objects.
Why would I want to use an object?
Oftentimes, you might find yourself needing to have multiple copies of a certain group of variables, each with different values for those variables. This can be easily done with the use of an object.
A hypothetical real world example could be that you are a fisherman who needs to keep track of his/her fish's sizes and weights. You would go about achieving this goal with objects.
All right, I understand what objects are, and why they are used, but I don't know how.
They really aren't that confusing. They're rather straightforward in fact.
First, you start out with a class. Let's go with the Fish example.
PHP Code
<?php
public class Fish { // Name it something so that we can easily keep track of what it is
}
Now we have a class, but there is nothing inside the class yet. Let's add some variables, such as the fish's length and weight.
PHP Code
<?php
public class Fish {
private int length; // An integer describing the fish's length
private int weight; // An integer describing the fish's weight
}
Now our fish can have a length and weigh! Yay! Now, we have to figure out how to create some "Fish" (the object). That's simple. Just add a construct.
PHP Code
<?php
public class Fish {
private int length;
private int weight;
public Fish(int length, int weight) { // We add two arguments in the construct so Java knows to give the fish a length and a weight, so that the Fish won't be lengthless nor weightless
this.length = length; // We have to assign the given length to the variable in our fish or else it wouldn't be there. The keyword this has to be used so that Java knows that you're talking about the length inside the fish, not the length that is being provided.
this.weight = weight; // Same as above
}
}
Great, now we can make new fish by simply evoking new Fish(someInt, someInt2); Unfortunately, we will have no way of finding out what the fish's length and weight because the variables are set to private. One way you could circumvent this problem would be by making the variables public, but that is generally frowned upon. Most people create setters and getters.
A typical setter and getter:
PHP Code
<?php
public int getLength() { // This goes into the Fish.java mentioned earlier. We say public int because we want it to ge accessed from anywhere, and because we want to show that the value being returned will be an integer
return length; // The keyword return sends whatever is after it back to whereever the function was called.
} // Remember to close your brackets :D
public void setLength(int newLength) { // Again with the public, but this time with a void instead of an int. A void is a function that does not return anything.
length = newLength; // This changes the length into the new one which is given when the function is called
} // Another bracket reminder. I forget a lot -_-
If you noticed, the construct looked kind of like the getter (the initial line did at least). It too uses public, but instead of int, it has Fish, which means that it returns a Fish (which does not actually have to be returned due to it being a construct).
Now, with all of this done, you can have a Fish object, and be able to find two integers inside of it. ( IN MY WORLD LENGTHS AND WEIGHTS CAN ONLY BE INTEGERS OK. In the real world though, using floats would probably be preferable because they support decimals )
To create a new Fish object, simply do this:
PHP Code
<?php
Fish yummyFish = new Fish(500, 500);
To get it's length, you can call yummyFish.getLength(), and the same for weight.
Thanks for reading this guide, and I hope you learned something :D
I may update this in the future to contain more complicated features of objects, such as inheritance, and of course, more about Fish!



![[Image: 9N6Aq1f.gif]](https://i.imgur.com/9N6Aq1f.gif)