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...