Frequently asked questions

Q- What is wrong with this method definition?

doSomething(){
   // Do something here
}

A- No return type is declared. You do not have to return a value from a method, but its return type must be void in this case. This is what the method should look like:

void doSomething(){
   // Do something here
}

Q- What is wrong with the following method definition?

float getBalance(){
   String customerName = "Linus Torvalds";
   float balance = 429.66f;
   return userName;
}

A- The method returns a string (userName), but the signature states that it must return a float. With a method name like getBalance, this code is what was likely intended:

float getBalance(){
   String customerName = "Linus Torvalds";
   float balance = 429.66f;
   return balance;
}

Q- When do we call the onCreate method? Trick question alert!

A- We don't. Android decides when to call onCreate, as well as all the other methods that make up the life cycle of an Activity. We just override the ones that are useful to us. We do, however, call super.onCreate, so that our overridden version and the original version both get executed.

Note

For the sake of complete disclosure, it is technically possible to call the life cycle methods from our code, but we will never need to in the context of this book. It is best to leave these things to Android.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.249.90