title: Can I define a variable globally then assign it locally and again use it another local method?
body: <p>Can I define a variable globally then assign it locally and again use it as another local method in Java? I tried to fetch data from FireBase database locally. Using it I want to perform some task in another local method but the problem is I could not manage to get the data to another method. Is there any way to do it?</p>
  
  <pre><code>double slatitude,slongitude,currbal;
          DatabaseReference mDatabase= FirebaseDatabase.getInstance().getReference();
          DatabaseReference mref,mlat,mlong,mstatus;
      protected void onCreate(@Nullable Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.travel);
              // Get the Intent that started this activity and extract the string
              Intent intent = getIntent();
              String id = intent.getStringExtra(rfidreader.EXTRA_MESSAGE);
              // Capture the layout's TextView and set the string as its text
              textView = (TextView) findViewById(R.id.message);
              //textView.setText(id+" "+"signed in successfully");
              mref=mDatabase.child(id).child("Balance");
              mlat=mDatabase.child(id).child("Start").child("Lat");
              mlong=mDatabase.child(id).child("Start").child("Long");
              mstatus=mDatabase.child(id).child("Status");
              getData(id);
  
          }
       public void getData(String input)
          {
              pssngrid = input;
              mDatabase.addValueEventListener(new ValueEventListener() {
                  @Override
                  public void onDataChange(DataSnapshot dataSnapshot)
                  {
                      currbal = dataSnapshot.child(pssngrid).child("Balance").getValue(Double.class);
                      status = dataSnapshot.child(pssngrid).child("Status").getValue(Integer.class);
                      mlatiude = dataSnapshot.child(pssngrid).child("Start").child("Lat").getValue(Double.class);
                      mlongittude = dataSnapshot.child(pssngrid).child("Start").child("Long").getValue(Double.class);
                  }
                  @Override
                  public void onCancelled(DatabaseError databaseError) {
  
                  }
              });
          }
  @Override
      protected void onStart() {
          super.onStart();
          if(currbal&gt;=10)
          {
              gps = new GPSTracker(onTravel.this);
              double slatitude = gps.getLatitude();
              double slongitude = gps.getLongitude();
              if(status==0)
              {
                  mlat.setValue(slatitude);
                  mlong.setValue(slongitude);
                  mstatus.setValue(1);
                  textView.setText("Thank you,you are eligible for travelling....");
  
  
              }
              else if(status==1)
              {
                  dis=HaverSineDistance(mlatitude,mlongitude,slatitude,slongitude);
                  fare=dis*5;
                  newbal=currbal-fare;
                  mref.setValue(newbal);
                  mstatus.setValue(0);
                  textView.setText("Distance Traveled:"+dis+"\n"+"Current Balance:"+currbal+"\n"+"Fare:"+fare+"\n"+"Net Balance:"+newbal);
  
              }
          }
          else
          {
              textView.setText("Sorry,you are not eligible for travelling this time.Please recharge your card..");
  
          }
  </code></pre>