0

I got this code:

mBoundService = ((LocalService.LocalBinder)service).getService();

Could some nice guy explain what does this mean :((LocalService.LocalBinder)service) And Could somebody please give some other example just like ((A)B).

Thank you.

AmyWuGo
  • 2,315
  • 4
  • 22
  • 26

4 Answers4

4

It's just casting. It's telling the compiler, "I know you only know about the value of this expression as type X, but I believe that at execution time it will be type Y. Check it for me at execution time, and then let me use it that way."

For example:

 Object x = getValueFromSomewhere();
 String text = (String) x; // I know x is a string reference really
 // Use text as a normal string reference

If your belief in the type involved is incorrect (e.g. if the value of x were a reference to an Integer instead of a String) then a ClassCastException is thrown.

See the Java Inheritance Tutorial for more information (or just search for "Java casting tutorial" to find lots of similar ones) or see section 15.16 of the Java Language Specification for the details.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

you have a variable (service) and you convert it to type (LocalService.LocalBinder) then you get the service this means that:

Service service;
LocalBinder binder = (LocalService.LocalBinder) service;
Service mBoundService = binder.getService();

try this it may help you

Reference

Example

Community
  • 1
  • 1
0

Local Binder means your activity should be binded to your application only. Please note that a service can be bound to other applications too. Whereas LocalService.LocalBinder allows your service to bound the application which contains the service.

ravan
  • 46
  • 2
0

This statement means that "service" variable casts to type LocalService.LocalBinder.

Other sample:

double a=1.1f;
int i=(int)a;// you can't write int i=a;
Alex Kucherenko
  • 20,168
  • 2
  • 26
  • 33
  • 2
    That's not a great example, IMO, because numeric conversions are somewhat different to the reference cast used here. – Jon Skeet May 18 '12 at 06:05