-8

In my android project I have several fields that I use in several activities, so I implement this class to hold them:

public class MainApplication extends Application {
        private UserId userId; 
}

also I can declare the UserId as static:

public class MainApplication extends Application {
        private static UserId userId; 
}

what is the difference between the two implementation? and which is preferable ?

edit,

I am asking this question because: currently I am declaring the userId as non static,and i am using it in activity number one and number tow.

but sometimes I get NullPointerException in activity number two although the userId was not null in activity number one. so I thought maybe if I declare it as static it will solve the problem ? thank you

user3648409
  • 293
  • 4
  • 10
  • 3
    -1, shows no research effort – Amanda S May 27 '14 at 20:33
  • no it is not duplicated, here there is a difference because I am extends the application class, so the fields will be declared just one time ? – user3648409 May 27 '14 at 20:36
  • There is no difference. A static field is a static field is a static field. The Application class is no different from any other class. – Gabe Sechan May 27 '14 at 20:36
  • it does not matter if you extend Application, you can also extend Activity or Fragment or anything else and it is still the same meaning. the meaning of static does not change based on what you extend – tyczj May 27 '14 at 20:38
  • you really need to start with java101, and don't skip the chapter on static fields. – njzk2 May 27 '14 at 20:49
  • @njzk2 please see the edit, just tell me if this will help me ! – user3648409 May 27 '14 at 20:52

1 Answers1

0

The difference is that static members are not instantiated and can be used directly like our main method in java as it can also run without making constructor. Declaring static means that this member will have only a single copy in memory.

Raheel Shahzad
  • 136
  • 2
  • 13