• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Slightly confused by syntax.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic