wood burning stoves 2.0*
The moose likes Java in General and the fly likes Cast inside object path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cast inside object path" Watch "Cast inside object path" New topic
Author

Cast inside object path

Nicholas Turner
Ranch Hand

Joined: May 24, 2003
Posts: 126
I have this item
public Object getItem( int index )
it returns an Employee class
for easiness it looks like this
public class Employee {
private String sName;
public String getName() { return sName;
}
so Lets say I have this line
System.out.println(Employees.getItem(1).name());
but this wont work cause its an object, how can I cast in the middle of the line like something like this ( and yes I know this wont work but how can I accomplish in one line)
Employees.getItem(1).(Employee)name();
can some one help?
thanks in advance,
nick
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Originally posted by Nicholas Turner:
Employees.getItem(1).(Employee)name();

You're close... try this:

That way you cast it into Employee before you try to execute getName()


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
 
 
subject: Cast inside object path
 
Similar Threads
Threads question
[immutable]...
Sooooo Confused!!!
Marcus Green Mock- doubt
Needs clarification about thread execution