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
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
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) ;
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()).s tart();
?
(new StudentNames.AddNames()).s
?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> (new StudentNames.AddNames()).s tart();
1. AddNames gets an argument
2. You can do that only for static inner classes.
1. AddNames gets an argument
2. You can do that only for static inner classes.
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
}
}
// (In StudentNames)
public void startIt() {
new Thread(new AddNames()).start();
}
private class AddNames implements Runnable {
public void run() {
// do it
}
}
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();
}
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();
}
:-)
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
}
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_argume nts).start ();
}
pass_here_the_arguments = fromAcct, toAcct, snames
Names snames = new Names ()
new AddNames(snames).start();
// here
new TransferStudent(pass_here_
}
pass_here_the_arguments = fromAcct, toAcct, snames
ASKER
Thanks a lot Aozarvo!!!
you have been most helpful
you have been most helpful
AddNames addNames = test.new AddNames();
addNames.start();