How to pass a variable to an inner class?

I want to upload an image at periodic intervals to an imageitem file. My outer class generates a url and I need to pass it to the inner class. How to achieve this?

public class MapTimer extends TimerTask{
    public void run() {
        System.out.println("Map starting...");

        String URL=null,serverquery=null;
        try {
            sendMessage(this.message);
            item.setLabel(item.getLabel()+"start");
            serverquery=receiveMessage();
            item.setLabel(item.getLabel()+"stop");
            URL = getURL(serverquery);               // my url to be passed to innerclass
            System.out.println("URl is "+serverquery);
            item.setLabel(URL+item.getLabel());
            Thread t = new Thread() {
            public void run() {
                    item.setLabel(item.getLabel()+"6");
                    try {
                          Image image = loadImage(URL);            // using url
                          System.out.println("GEtting image....");
                          item = new ImageItem(null, image, 0, null);
                          form.append(item);
                          display.setCurrent(form);

                    } catch (IOException ioe) {
                          item.setLabel("Error1");
                    }
                    catch (Exception ioe) {
                          item.setLabel("Error1");
                    }
              }
         };
         t.start(); // write post-action user code here
     }
     catch(Exception e){
         System.out.println("Error3"+e);
     }
  }    
}

How to pass a url to my innerthread class?

+3
source share
4 answers

You must declare the variable final or not use a variable other than a field in the class.

public class YourClass {

private String url;

public void yourMethod {
   url = getURL(serverquery);
   System.out.println("URl is "+serverquery);
   item.setLabel(URL+item.getLabel());
   Thread t = new Thread() {
      public void run() {
         item.setLabel(item.getLabel()+"6");
         try {
           Image image = loadImage(url);            // using url
           System.out.println("GEtting image....");
           item = new ImageItem(null, image, 0, null);
           form.append(item);
           display.setCurrent(form);

    } catch (IOException ioe) {
      item.setLabel("Error1");
    }
                catch (Exception ioe) {
      item.setLabel("Error1");
    }

  }
};
t.start(); // write post-action user code here
}

}

+5
source

It should work if your URL is in the final link. This means that the link will not move. Therefore, you cannot initialize it to zero, and then call getURL, you need to declare it at this point:

final String URL = getURL(serverquery);
+1
source

, ( ) , ( URL)

1) :

, , , (, URL) , /URL .

public class MapTimer extends TimerTask{
   public void run() {
     ...
   }
   private URLGenerator urlGenerator = null;
   public MapTimer(URLGenerator urlGen){
      ...
   }

URL-, ( if (urlGen!= null), )

2)

, , ( ..)

public class MapTimer extends TimerTask{
       public void run() {
         ...
       }
       private URL urlToLoad = null;
       public void setURL(URL urlToLoad){
          ...
          //store and wake up thread
       }

, , , (, , ..)

,

0

Just make the field a static variable belonging to the class, and not any instance of the class object, for example:

public YourClass {

    private static String url;

    //...
}

Note that the static variable will be used for all instances of the class object.

0
source

Source: https://habr.com/ru/post/1790296/


All Articles