-1

I want to assign Calendar or Date object which is a global variable in another class. In class Class1 I initialize date object like his:

Calendar date1;
date1 = Calendar.getInstance();

In Class2 I do this:

Calendar date2 = Class1.date1;

Why does it work good if I assign Class1.date1 to date2 inside any method in Class2 but doesn't work if I declare date2 as global variable and assign value Class1.date1? (In this case there will be NullPointerException thrown.)

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
jack_017
  • 45
  • 1
  • 5
  • This is probably a problem with the initialization order of the two classes. Such could for instance happen if they both depend on each other. We would need a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) to be able to tell for sure. – Ole V.V. Apr 29 '17 at 09:38

1 Answers1

0

You simply can't update a global variable without static with another anytype variable because global variables are also class instances and they have to be static to save all the changes that you done on them.

  • the variable date1 is public and static; variable date2 is static but I am still getting NullPointerException. – jack_017 Apr 28 '17 at 22:50
  • Then you should look [this](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) place to fix it. Because this case has nothing to do about NullPointerException. – Gürtuğ Güngör Apr 28 '17 at 22:51