Epnix.Com

A place to test and try things out

Browsing Posts tagged Objects

More About Objects

No comments

Here is my Code:

class Horse {

int HairColor;
String Name;
}

public class RefObjTest {

public static void main (String [] args) {
Horse a = new Horse();
Horse b = new Horse();
Horse c = b;

System.out.println(“We have 3 Horses”);
a.Name = “A”;
b.Name = “B”;
System.out.println(“The first horse is ” + a.Name + “, the second is ” + b.Name + “, and the last ones’ name is ” + c.Name + “.”);
System.out.println(“Changeing Horse c’s Name”);
c.Name = “C”;
System.out.print(“The first horse is ” + a.Name + “, the second is ” + b.Name + “, and the last ones’ name is ” + c.Name + “.”);

}
}

And Here is my Out put.

We have 3 Horses
The first horse is A, the second is B, and the last ones’ name is B.
Changeing Horse c’s Name
The first horse is A, the second is C, and the last ones’ name is C.

The good news is, I can see what’s going to happen.  What I don’t understand is, Why would I want this?  If I’m looking at houses on the street, or horses in the barn, Seems like it would be really inefficient to create a full object for each one. Hmmm Well I suppose it could be just where I’m at in the book, maybe there’s a better way of doing things down the way as far as utilizing the classes that are made.

Ok, I’m going the spare you the Why this question comes to mind, but I’m still going to ask the question. Can Elephants Jump?  I just put that question into Facebook / twitter as this Chapter starts out talking about putting a Giraffe into a Rabbit variable.  and calling Rabbit hopper = new Giraffe(); .  So I thought yeah, might be interesting seeing a Giraffe hopping, but then thought oooh what would be even cooler was seeing a Elephant jump… If they could how much would the ground shake?  Ok Back to java.

Variables in themselves are pretty straight forward until they talk about Object reference variables.  I think I know what they mean, but the examples they are giving are not helping at all. So I’m stuck trying to sort though what is correct.  It’s these times that I wish I had a Instructor or at least a mentor that can say ok that example is causing more confusion, here look at it THIS way, until it finally make sense.  Or they could listen to what I’m thinking and say yep, you actually got it, ignore that example.

What I’m looking at is this.

Horse a = new Horse();
Horse b = new Horse();

To me, I’m looking at 2 instances of the object Horse.  One instance is called “a” one instance is called “b”. And lets say I sent some color Instance Variable on “a” to be red and on “b” it’s brown.  Let me Show the whole Code:

Horse a = new Horse();
Horse b = new Horse();

a.haircolor = red;
b.haircolor = brown;

So how I have 2 objects, that are instances of Horse.  According to the book I have 2 Horse Objects, and if I did:

Horse c = b;

To me I would now have 3 instances of the horse object with b and c being exact duplicates of each other.  According to this book though I only have 2 objects as b and c reference the same object.  The difference is, if both b and c are referencing the same object then if I make a change to a variable in b wouldn’t it also be changed in c?

Looks like time to let Google do some walking.