[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

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
0
ZURINET
Asked:
ZURINET
1 Solution
 
aozarovCommented:
StudentNames test = new StudentNames();
AddNames addNames = test.new AddNames();
addNames.start();
0
 
aozarovCommented:
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);

0
 
InteractiveMindCommented:
Can't you just do this?

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

?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
aozarovCommented:
I guess this is actually what you are looking for:

public StudentNames () {
               Names snames = new Names ()
               new AddNames(snames).start();
               }
0
 
aozarovCommented:
>> (new StudentNames.AddNames()).start();
1. AddNames gets an argument
2. You can do that only for static inner classes.
0
 
CEHJCommented:
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
            }
      }
0
 
ZURINETAuthor Commented:
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();
               }
0
 
aozarovCommented:
:-)
0
 
ZURINETAuthor Commented:
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
               }
0
 
aozarovCommented:
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
0
 
ZURINETAuthor Commented:
Thanks a lot Aozarvo!!!

you have been most helpful
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now