Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cast inside object path

 
Nicholas Turner
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic