• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Slightly confused by syntax.

 
Caitlin Gibson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
In the following code segment:
static int find( Object[] a, Object key )
{
int i;
for( i = 0; i < a.length; i++ )
{

if( a[i].equals(key))
return i;
}
return -1;
}

then...

Employee e = new Employee[10];
Employee harry;
...
int n = find( staff, harry);

Harry doesn't point to anyhing does he? I am assuming 'harry' is an object variable...
When harry is 'assigned' to key ( which is object reference, does that instantiate him in a sense?

Also, when you have a code segment like this:
Classname var = obj.method_call();
How is that read? Does the method call return an object of type Classname that you are assigning to var?
Thanks in advance,
-Caitlin
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Caitlin:
When harry is 'assigned' to key ( which is object reference, does that instantiate him in a sense?

No. The variable harry is uninitialized and using it in a reference will never cause it to instantiate. In fact, the code you showed should trigger a compiler error on the first use of harry complaining about its uninitialized state.
How is that read? Does the method call return an object of type Classname that you are assigning to var?

Close! The method can return any object of a type that is assignable to Classname. That is, the object might be a Classname, it might be an object derived from Classname, or it could be an object that implements Classname if Classname is an interface.
hth,
bear
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. If you surround code you post in this forum with the UBB code tags, it makes it easier for all to read as its formatting will be preserved.
 
Caitlin Gibson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
Thanks for the info.
Could you ( or someone else ) provide a few small examples of the things you mentioned? I am less confused now than before but not enough to say I uinderstand completely.
Thanks,
Caitlin.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Here are some simple examples.
After this line:

you have created an uninitialized reference to an Employee object. No Employee object yet exists. In order to be able to use the harry variable to reference an Employee object you need to assign one to the reference.
If you are going to create a new Employee, creating one with a constructor could look like:

or, if there's a method that will return an Employee reference:


Once an object has been assigned to harry, you can use the variable to reference the object.
Does this help?
bear
[ March 21, 2003: Message edited by: Bear Bibeault ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic