title: Unexpected behaviour by class variable in android app
body: <p>In my <code>RegistrationActivity.java</code> class file I've declared a <code>numOfUsers</code> variable to count the number of users in my database.</p>
  <pre><code>
  import ...
  
  public class RegistrationActivity extends AppCompatActivity {
  
     //Other declarations
      private static long numOfUsers;
  </code></pre>
  <p>I've created an event listener for checking if a particular user exists in the database. Inside this event listener there is another event listener which counts the total number of users in the database.</p>
  <pre><code>        ValueEventListener eventListener = new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  if(!dataSnapshot.exists()) {
                      //create new user
                      database.addListenerForSingleValueEvent(new ValueEventListener() {
                          @Override
                          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                              if (dataSnapshot.exists()) {
                                  numOfUsers = dataSnapshot.getChildrenCount();
                                 
  
  
                              }
  
  
                          }
                        
                          @Override
                          public void onCancelled(@NonNull DatabaseError databaseError) {
                          }
                      });
  //Displaying the numOfUsers in app
                      userinfo.setText(String.valueOf(numOfUsers));
  
  
  
                  }
  </code></pre>
  <p><strong>This prints 0.</strong> If I place <code>userinfo.setText(String.valueOf(numOfUsers));</code> inside the second event listener then everything works fine.</p>
  <pre><code>        ValueEventListener eventListener = new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  if(!dataSnapshot.exists()) {
                      //create new user
                      database.addListenerForSingleValueEvent(new ValueEventListener() {
                          @Override
                          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                              if (dataSnapshot.exists()) {
                                  numOfUsers = dataSnapshot.getChildrenCount();
  //This works perfectly fine
                                  userinfo.setText(String.valueOf(numOfUsers));
                                 
  
  
                              }
  
  
                          }
                        
                          @Override
                          public void onCancelled(@NonNull DatabaseError databaseError) {
                          }
                      });
  
                    
  
  
  
                  }
  </code></pre>
  <p>I don't understand why this is happening. <code>numOfUsers</code> is a static class variable I should be able to access it's value from anywhere inside the class. Is there a way I can't print <code>numOfUsers</code> outside the second event listener?</p>