aspose file tools*
The moose likes Beginning Java and the fly likes Slightly confused by syntax. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Slightly confused by syntax." Watch "Slightly confused by syntax." New topic
Author

Slightly confused by syntax.

Caitlin Gibson
Greenhorn

Joined: Nov 25, 2002
Posts: 12
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Nov 25, 2002
Posts: 12
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Slightly confused by syntax.