views:

90

answers:

5

Just when I think Im starting to understand the basics, I find something that brings me right back to reality. In this case, typed reference.

I found an example similar to this:

class Worker
{
Boss boss;

public void Advise(Boss pBoss)
{
    this.boss = pBoss;
}

How can you reference methods within the Boss class if its not static and not instantiated?

I guess my real question is whats the difference between:
Boss boss;
and
Boss boss = new Boss();

Thank you,
FS

+6  A: 

Boss boss; creates a field called boss of type Boss (which has the value null by default).

Boss boss = new Boss(); creates a variable called boss of type Boss and stores a reference to a new instance of the type Boss in that variable.

Daniel Renshaw
@code4life: in the example given boss is a *field* not a *variable*. Fields are initialised to their default value.
Daniel Renshaw
Ack, my apologies, I jumped in too fast. I'm retracting my comment!
code4life
@Daniel: This is probably splitting hairs, but I think that technically speaking a field is considered a kind of variable. There are seven variable categories listed here: http://msdn.microsoft.com/en-us/library/aa691161(v=VS.71).aspx two of which (static variables, instance variables) are fields. Your point that fields are automatically initialized (unlike other variable categories, except array elements) is of course a good one.
Tim Goodman
@Tim: thanks for the link; I'd not seen it laid out like that - I've always used "variable" and "local variable" as synonyms. Curiously MS don't use the term "field" in that list at all but it is used in the pages linked to from "instance variable" and "static variable" (which I've always referred to as "instance field" and "static field").
Daniel Renshaw
+1  A: 

Just having the code Boss boss; is only going to give you the ability to create a class of type Boss. When you instantiate the class using the code Boss boss = new Boss(); or by setting the variable this.boss = pBoss; in your Advise method, you will then be able to access methods and properties on your instantiated instance of the Boss object.

Steve Danner
so is this.boss = pBoss the same as Boss boss = new Boss()?
Farstucker
@Farstucker: No, `this.boss = pBoss` stores the value of `pBoss` in `boss`. The value of `pBoss` is whatever you pass to `Advise`. You could pass in `new Boss()`, or you could pass a reference to some existing instance of `Boss`.
Tim Goodman
A: 

The Advise() method takes an instance of the Boss class and then sets the private field to that instance of Boss. Once the Advise method has been called and the Boss instance set-up, the Worker class can then use any methods or properties the Boss exposes.

Peter Kelly
+1  A: 

The field boss can contain a reference to an instance of the class Boss. Initially, boss contains null, meaning it doesn't reference any instance. Saying new Boss() creates a new instance of Boss. You can store a reference to this new instance in boss.

pBoss also can contain a reference to an instance of Boss, and you can store this reference in boss by saying boss = pBoss.

Tim Goodman
If you're going to downvote, please explain why.
Tim Goodman
+1  A: 

How can you reference methods within the Boss class if its not static and not instantiated?

You can't. Except for built-in datatypes like int, you always have to instantiate variables.

this.pBoss = Boss; only works when an object of type Boss has been instantiated somewhere else and is being passed into Advise() as an argument. If it had been called this way:

Advise(null);

...then you still couldn't use Worker.boss (it would throw an exception).

For what it's worth, this is more obvious in a lower-level language like C++.

egrunin
Youre right, I looked at the remainder of the code and noticed it was instantiated in another part of the class. Thank you.
Farstucker