wood burning stoves*
The moose likes Android and the fly likes why initialize variables the way they do in Android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "why initialize variables the way they do in Android" Watch "why initialize variables the way they do in Android" New topic
Author

why initialize variables the way they do in Android

x kevik
Greenhorn

Joined: Dec 17, 2012
Posts: 2
I am curious to why certain things are done in android and why they are done in that way. What is the advantage? One of these things is the way that variables are initialized in Android. I see many examples on the net and in books where they define the variable in the beginning of the class and never give it an initial value. then later they will initialize the variable in the onCreate method of the activity. Why not define it and initialize it in only one line like this: Int x = 0; or Button b = (Button) findViewById(buttonFromXML);

instead it is always split in two like this;

Integer x;
Button b;

@Override
public void onCreate() {

x = 0;

b = (Button) findViewById(buttonFromXML);

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41182
    
  45
The button (or whatever UI component) only exists once the layout that contains it has been created. That's usually one of the first things that happens in the onCreate method.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why initialize variables the way they do in Android
 
Similar Threads
Garbage Collection Doubt
packages and CLASSPATH
performance overhead for setters and getters in Android?
Trying to get manageable arrays to work with
usage of "this"