What are classes, references and objects?

71,777

Solution 1

If you like housing metaphors:

  • a class is like the blueprint for a house. Using this blueprint, you can build as many houses as you like.
  • each house you build (or instantiate, in OO lingo) is an object, also known as an instance.
  • each house also has an address, of course. If you want to tell someone where the house is, you give them a card with the address written on it. That card is the object's reference.
  • If you want to visit the house, you look at the address written on the card. This is called dereferencing.

You can copy that reference as much as you like, but there's just one house -- you're just copying the card that has the address on it, not the house itself.

In Java, you can not access objects directly, you can only use references. Java does not copy or assign objects to each other. But you can copy and assign references to variables so they refer to the same object. Java methods are always pass-by-value, but the value could be an object's reference. So, if I have:

Foo myFoo = new Foo();     // 1
callBar(myFoo);            // 2
myFoo.doSomething()        // 4

void callBar(Foo foo) {
    foo = new Foo();       // 3
}

Then let's see what's happening.

  1. Several things are happening in line 1. new Foo() tells the JVM to build a new house using the Foo blueprint. The JVM does so, and returns a reference to the house. You then copy this reference to myFoo. This is basically like asking a contractor to build you a house. He does, then tells you the house's address; you write this address down.
  2. In line 2, you give this address to another method, callBar. Let's jump to that method next.
  3. Here, we have a reference Foo foo. Java is pass-by-value, so the foo in callBar is a copy of the myFoo reference. Think of it like giving callBar its very own card with the house's address on it. What does callBar do with this card? It asks for a new house to be built, and then uses the card you gave it to write that new house's address. Note that callBar now can't get to the first house (the one we built in line 1), but that house is unchanged by the fact that a card that used to have its address on it, now has some other house's address on it.
  4. Back in the first method, we dereference myFoo to call a method on it (doSomething()). This is like looking at the card, going to the house whose address is on the card, and then doing something in that house. Note that our card with myFoo's address is unchanged by the callBar method -- remember, we gave callBar a copy of our reference.

The whole sequence would be something like:

  1. Ask JVM to build a house. It does, and gives us the address. We copy this address to a card named myFoo.
  2. We invoke callBar. Before we do, we copy the address written on myfoo to a new card, which we give to callBar. It calls that card foo.
  3. callBar asks the JVM for another house. It creates it, and returns the new house's address. callBar copies this address to the card we gave it.
  4. Back in the first method, we look at our original, unchanged card; go to the house whose address is on our card; and do something there.

Solution 2

When you code, you build an

Instance (occurrence, copy)

of an

Object

of a said

Class

and keep a

reference

to it, so you can call its methods.

Also, some OOP basics: Classes, Object, Instance, and Reference.

Solution 3

In the book "Thinking in Java" from Bruce Eckel it has been described perfectly:

"You might imagine a television (the object) and a remote control (the reference). As long as you’re holding this reference, you have a connection to the television, but when someone says, “Change the channel” or “Lower the volume,” what you’re manipulating is the reference, which in turn modifies the object. If you want to move around the room and still control the television, you take the remote/reference with you, not the television.

Also, the remote control can stand on its own, with no television. That is, just because you have a reference doesn't mean there’s necessarily an object connected to it. So if you want to hold a word or sentence, you create a String reference:

String s;

But here you’ve created only the reference, not an object. If you decided to send a message to s at this point, you’ll get an error because s isn’t actually attached to anything (there’s no television). A safer practice, then, is always to initialize a reference when you create it:

String s = "asdf";

However, this uses a special Java feature: Strings can be initialized with quoted text. Normally, you must use a more general type of initialization for objects.

When you create a reference, you want to connect it with a new object. You do so, in general, with the new operator. The keyword new says, “Make me a new one of these objects.” So in the preceding example, you can say:

String s = new String("asdf");

Not only does this mean “Make me a new String,” but it also gives information about how to make the String by supplying an initial character string. Of course, Java comes with a plethora of ready-made types in addition to String. What’s more important is that you can create your own types. In fact, creating new types is the fundamental activity in Java programming."

Solution 4

Suppose you write there two lines of code:

Engine app1 = new Engine(); //LINE 1

Engine app2  = app1; //LINE 2

In line 1, Engine is a class, its a blue-print basically.

new Engine() is the instance that is made on the heap.

You are refering that instance by using app1 and app2 in your code.

So app1 and app2 are the references.

Solution 5

When you create an object, what happens behind the scene is that a piece of memory is reserved for containing that object. This could be anywhere in the great big memory landscape; it's up to the operating system and the compiler, and you don't really have any control or knowledge of where it ends up.

Ask yourself, then, how do you use that object if you don't know where in memory it is? How can you read a value from it if you don't know where that value is stored? This is what references do for you. They are a way of keeping in touch with the object. It's a little string attached to the balloon that is a reference.

You use the reference to say that "I want to touch this object now!", or "I want to read a value from this object!".

Share:
71,777
user962206
Author by

user962206

Updated on July 09, 2022

Comments

  • user962206
    user962206 almost 2 years

    I've been programming java for 2 years now, and apparently I have encountered a problem where I couldn't understand and differentiate class, reference, and an object again (I do not get why I forget these concepts).

    Lets get to down to the problem, which is that I am not sure if a class or reference are the same, though I have already an idea what is object.

    Can someone differentiate in a nice and understandable and complete manner what are classes, references and object?

    All I know is that the class is more like of a template for an object (blueprint to a house where the class is the blueprint and the house is an object).

  • user962206
    user962206 about 12 years
    what does it mean when you "keep" a reference? is it like the implementation of that object? or the object is the implementation itself?
  • Marcelo
    Marcelo about 12 years
    @user962206 A reference is nothing more than 'pointers' to where said object is, in-memory. One could say that you write the 'implementation' in the Class file, not sure that's what you mean?
  • user962206
    user962206 about 12 years
    That's what I mean, but if you could, can you explain more details to me about reference? I do get the object and class but not the reference
  • user962206
    user962206 about 12 years
    if you mean instance, you mean object?
  • user962206
    user962206 about 12 years
    Okay thanks I finally get it now :)! Car myCar = new Car() myCare is a reference right?
  • Marcelo
    Marcelo about 12 years
    @user962206 Exactly! It is merely the address of the Car object you created on your machine's memory, so you can call its methods!
  • user962206
    user962206 about 12 years
    Thank you, one last thing. are instance and Objects the same?
  • Marcelo
    Marcelo about 12 years
    @user962206 Nope: you always have an instance (or an occurrence, or a copy) of your object. Do also notice Java has a class called Object, which is the parent class in the hierarchy for all other classes written.
  • yshavit
    yshavit about 12 years
    @Marcelo Slight nitpick: the reference isn't the address of the Car object, it's a place where you can store a copy of the address. If you modify the reference, it doesn't change the Car's address -- just means you're now pointing to a different object (or null, which is no address).
  • user962206
    user962206 about 12 years
    last question so basically. myFoo is where you store the reference of a said object?
  • yshavit
    yshavit about 12 years
    @user962206 You're welcome. :) As for your question, basically: In colloquial lingo, myFoo is the reference, which is where you store the address of an object. (You never actually see what that address is in Java, btw -- you just know it's there, and that the JVM knows how to look at it.) If you read the JLS though, they'll say that myFoo is a variable of reference type, and that it stores the reference to the object.
  • user962206
    user962206 about 12 years
    does this concept also apply for C#? except about the (JLS) ?
  • yshavit
    yshavit about 12 years
    @user962206 I'm not sure, never got really into C#. But I believe C# is a superset of C++, in which references are something else entirely. Java references are closer to C++ pointers. So, I don't know what C# calls its version of Java references.
  • user962206
    user962206 about 12 years
    do you know any place where there is an OOP concepts guide like (Classes,references, and objects) regardless of programming language? just OOP concepts in general ? but if you could explain them to me regardless of the language please do :)
  • yshavit
    yshavit about 12 years
    @user962206 don't know, sorry. Maybe wikipedia?
  • Quazi Irfan
    Quazi Irfan about 9 years
    What's the difference between reference and address? It feels you are using them somehow interchangeably. From this answer, 'reference to pointer' is used to dereference a variable. But what is a reference? A number? or a pointer pointing towards a pointer?
  • yshavit
    yshavit about 9 years
    @iamcreasy They're very similar. An address is the place in the JVM (that is, in memory) where the object lives. A reference is a variable that points to this address. The analogy would be like asking what's the difference between an integer and an int -- an integer (or address) is a thing you want stored, and an int (or reference) is the thing that stores it.
  • BenKoshy
    BenKoshy about 8 years
    I'm from a .net background, but am a little confused: the foo variable is passed to the Callbar method. In other words, my friend Callbar builds a new house at the address of the existing myFoo house. Doesn't he demolish the myFoo house to build the new Foo house? In other words, isn't the myFoo variable now referring to the new house that has been built by CallBar, rather than the old house it originally referred to?
  • yshavit
    yshavit about 8 years
    @BKSpurgeon Callbar wasn't given a house, it was given a card on which was written the address of a house. So, it didn't build a new house at that address -- it built a new house, and then wrote the address of that house on the card. That means that Callbar can't find the old house any more -- but nobody else is affected. The old house is still there, and anyone who had other cards with the old house's address can still find it.
  • BenKoshy
    BenKoshy about 8 years
    @yshavit thank you for your clarification. I am following you 100% till you say this - then i get confused: Call bar can't find the old house anymore. agreed. "But nobody else is affected"? Would not the old house address cards now point to the callbar house instead? I"ll check this by code and get back to you with an answer because it is puzzling to me.
  • BenKoshy
    BenKoshy about 8 years
    @yshavit you are 100% correct. i cannot believe i had misinterpreted the fundamentals like that.
  • Exigente05
    Exigente05 over 7 years
    @BKSpurgeon, Java methods are always pass-by-value (object reference). So when callBar(myFoo); invoked only value (reference) is being passed and "foo"'s scope is only within callBar. And foo is getting new Foo() only. So foo is referencing new one - I guess I am right.
  • Jeffrey
    Jeffrey almost 3 years
    So app2 does not contain a copy of the engine created on line 1, it holds a copy of the reference that points to the engine.
  • Jeffrey
    Jeffrey almost 3 years
    Also a Null Pointer Exception is thrown when the reference to the engine returns null or does not point to anything.