aspose file tools
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
Register / Login


JavaRanch » Java Forums » Mobile » Android
Reply 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: 35446
    
    9
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.


Android appsImageJ pluginsJava web charts
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: why initialize variables the way they do in Android
 
Similar Threads
usage of "this"
Garbage Collection Doubt
packages and CLASSPATH
performance overhead for setters and getters in Android?
Trying to get manageable arrays to work with