Link to home
Start Free TrialLog in
Avatar of ZURINET
ZURINET

asked on

Inner class

How do I access the inner class AddNames and start the tread.. or should I overwrite the run method.?


public class StudentNames {
      public static void main (String[] args) {
StudentNames test = new StudentNames();
}


            public StudentNames () {
                  Names snames = new Names ()
            
                  }
public class AddNames extends Thread {
      Names nameAA;
public AddNames(Names a) {
      nameAA = a;
}
public void run () {
int total = nameAA.addNames ();
System.out.println ("ADD: total = " + total);
}

}

Thanks in Advance

Ed
Avatar of aozarov
aozarov

StudentNames test = new StudentNames();
AddNames addNames = test.new AddNames();
addNames.start();
instead of calling the empty constructor AddNames();
You will need to call the constructor that gets Names

e.g. AddNames addNames = test.new AddNames(a_names_instance);

Can't you just do this?

(new StudentNames.AddNames()).start();

?
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> (new StudentNames.AddNames()).start();
1. AddNames gets an argument
2. You can do that only for static inner classes.
Avatar of CEHJ
You'd be better letting StudentNames handle that. Also it's better to implement Runnable than to subclass thread:

// (In StudentNames)


public void startIt() {
      new Thread(new AddNames()).start();
}

      private class AddNames implements Runnable {
            public void run() {
                  // do it
            }
      }
Avatar of ZURINET

ASKER

Thanks all for your help!!!


This code is what  solve the problem.. from aozarov

 I guess this is actually what you are looking for:

public StudentNames () {
               Names snames = new Names ()
               new AddNames(snames).start();
               }
:-)
Avatar of ZURINET

ASKER

Aozarov just one last question for you

how do I start this  tread below  inside the //commented area



public class TransferStudent extends Thread {
      int from, to, studentLoan;
            Names Qnames ;
    public TransferStudent(int fromAcct, int toAcct, int I, Names a) {
          from = fromAcct; to = toAcct; studentLoan = I; Qnames = a;
}
        public void run () {
        Qnames.transfer (from, to, studentLoan);
}
}








public StudentNames () {
               Names snames = new Names ()
               new AddNames(snames).start();
               // here
               }
public StudentNames () {
               Names snames = new Names ()
               new AddNames(snames).start();              
                 // here
               new TransferStudent(pass_here_the_arguments).start();
               }
pass_here_the_arguments = fromAcct, toAcct, snames
Avatar of ZURINET

ASKER

Thanks a lot Aozarvo!!!

you have been most helpful