简体   繁体   中英

call method inside a class

Greetings

I'm developing my first app in java and I wanted to know how can I do the following

I created this

public class Test
{
     ...

     public void control()
     {
          final ArrayList<Module> mods = new ArrayList<Module>();
          if (i == 0)
          {
               mods.add( null );
               mods.add(moduleList.get( i ));
               Thread t = new Thread( new Runnable()
               {
                    public void run()
                    {
                         StartController(mods);
                    }             
               });     
          }
     }

     public void StartController(ArrayList<Module> modList)
     {
          //Do Stuff
     }
}

But I am unable to accomplish this! he can't find StartController....

I would like to keep the code close to this. Is there a way to do that?

That looks 100% correct but a common solution for this, in situtations where that type of call doesn't work, is to do:

Test.this.StartController(mods)

instead of

StartController(mods)

try to use ExecutorService and the Runnable interface instead. You can see an easy example right here:

http://programmingexamples.wikidot.com/executorservice

Another option could be to make the StartController(..) method static.

public static void StartController(ArrayList<Module> mods) {
  // do stuff
}

And then change the call to:

Test.StartController(mods);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM