title: Java function return a value before the function is executed
body: <p>I'm currently developping an android app which is accessing to an online API. I have a class dedicated to this task, and another class that just use the informations retrieved from the API. The problem is that when I call the API access class, it has to be asynchronous (defined by android studio) so I use a new Thread, but the return in the class that use the data is null, when the API access class return the good result.</p>
  
  <p>I already tried to join the two threads with thread.join() but it doesn't work.</p>
  
  <p>Here is the function that access the API in the API access class.The System.out at the end works as expected (I see the good result in the console)</p>
  
  <pre><code>Thread t = new Thread() {
              public void run() {
                  try {
                      String url = "-----------------------------------------------"+id;
                      URL obj = new URL(url);
                      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  
                      con.setRequestMethod("GET");
  
                      con.setRequestProperty("x-api-key", "-------------------------------");
                      int responseCode = con.getResponseCode();
  
                      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                      String inputLine;
                      StringBuffer response = new StringBuffer();
  
                      while ((inputLine = in.readLine()) != null) {
                          response.append(inputLine);
                      }
                      in.close();
                      HttpRequest.retour = new JSONArray(response.toString());
                      System.out.println(HttpRequest.retour.toString());
                  }catch(Exception e){
                      e.printStackTrace();
                  }
              }
          };
          t.start();
  </code></pre>
  
  <p>But when I try to do that in another class :</p>
  
  <pre><code>System.out.println(retour.toString());
  
  </code></pre>
  
  <p>I get a null pointer exception because the return of the previous method is null.</p>
  
  <p>What's my mistake here?</p>