0

I'm confused by the following code that I'm learning for my class. I'm wondering if someone could break down the following part for me, in an Explain Like I'm Five fashion:

(ICustomerSvc) factory.getService(ICustomerSvc.NAME);

Essentially, I'm confused as to why the ICustomerSvc and ICustomerSvc.NAME are in parentheses.

public void create (Customer cust) throws ServiceLoadException, CustomerSvcException {
    Factory factory = Factory.getInstance();
    ICustomerSvc custSvc = (ICustomerSvc) factory.getService(ICustomerSvc.NAME);
    custSvc.store(cust);
}
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 3
    `(ICustomerSvc)` is a cast, look it up. `(ICustomerSvc.NAME)` is a method argument. – Rob Spoor Aug 01 '22 at 16:04
  • Does this answer your question? [What does a type name in brackets mean?](https://stackoverflow.com/questions/23237706/what-does-a-type-name-in-brackets-mean) – Ole V.V. Aug 01 '22 at 16:16
  • I think the link you provided is a little high-level for me to understand right away, but with some more looking into it, I'm sure it'll eventually make sense. Looks like I need to do more researching on casting. Thank you for this link! –  Aug 01 '22 at 16:24
  • Does this answer your question? [What does a type name in brackets mean?](https://stackoverflow.com/questions/23237706/what-does-a-type-name-in-brackets-mean) – AddeusExMachina Aug 03 '22 at 18:08

1 Answers1

1
ICustomerSvc custSvc = (ICustomerSvc)factory.getService(ICustomerSvc.NAME);

(ICustomerSvc) is casting the return value of factory.getService(ICustomerSvc.NAME) so that it can be assigned to a variable of type ICustomerSvc.

This is presumably necessary because factory.getService returns some other type, e.g. CustomerSvc or Service. I can't say exactly because you haven't shown the definition.

If that return value isn't an instance of ICustomerSvc (or null), that will fail with a ClassCastException.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Ok, this makes sense. In learning so much new and confusing information about coding, I vaguely remember learning about casting. I will go and look that up. I appreciate you, thank you! –  Aug 01 '22 at 16:08
  • Also, in Eclipse, do you know if there are ways to customize your view in which you can hover your cursor over components, such as casting, and it can explain to you the function of the component? Or is there a way to look this up in Eclipse? ie, if I were to hover my cursor over (ICustomerSvc), or maybe even highlight it, Eclipse would be able to explain that it's a cast? –  Aug 01 '22 at 16:16
  • Sorry, don't use Eclipse. – Andy Turner Aug 01 '22 at 16:16
  • *in which you can hover your cursor over components, …, and it can explain to you the function of the component?* I don’t think that exists in standard Eclipse. There *might* be a plugin for it, just speculating. – Ole V.V. Aug 01 '22 at 16:18
  • So, to clarify my idiotic question, when you hover the cursor over certain items, like ``String``, Eclipse will offer information about them. I was just wondering if there was something to that effect for syntaxes as well, so that I don't have to resort to being the Village Idiot here on stackoverflow :) Thanks! –  Aug 01 '22 at 16:28