Results 1 to 10 of 10
- 09-19-2009, 05:21 PM #1
Senior Member
- Join Date
- Oct 2008
- Posts
- 116
- Rep Power
- 0
how I transfer/use vars or/and methods from a Thread subclass -to-> the class of main
how I transfer/use vars or/and methods from a Thread subclass -to-> the class of main() and vice versa ?
a subclass like":
class MyThread extends Thread
{
may contain getters / setters run from main() to get values from subclass ?
- 09-19-2009, 05:49 PM #2
When you create an instance of your Thread in the main() the main() will have access to the public methods of your thread object.
Java Code:public static void main(String[] args) { MyThread myThread = new MyThread(); myThread.setSomeValue("blah"); System.out.println( myThread.getSomeValue() ); myThread().start(); }Last edited by mrmatt1111; 09-19-2009 at 05:51 PM.
My Hobby Project: LegacyClone
- 09-19-2009, 06:29 PM #3
Senior Member
- Join Date
- Oct 2008
- Posts
- 116
- Rep Power
- 0
If I have:
If I have:
public String getAd() { return ad; }
in a getter and
ad var
setup via
String ad = input.nextLine();
in run()
running in main()(other class) the
x=advt.getAd();
it will work ? where is error ? when call the
x=advt.getAd();
prior or after advt.start();
???
- 09-19-2009, 08:13 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Forget writing threads for now. Get yourself a decent Java tutorial and learn the basics first including how to create instances and how to call methods.
- 09-19-2009, 08:37 PM #5
Senior Member
- Join Date
- Oct 2008
- Posts
- 116
- Rep Power
- 0
I know that well and did it but on threads no compile?
C:\JavaAdv\Lesson6\Assign61>javac Advertisement.java
Advertisement.java:41: cannot find symbol
symbol : variable x
location: class Advertisement
public boolean getAd() { return x; }
^
1 error
C:\JavaAdv\Lesson6\Assign61>
-
There is no "x" variable in scope within the getAd method. Again, I have to agree with r...x as this is a very basic aspect of Java that you need to know before messing with threads. You would benefit greatly by studying a basic Java tutorial.
- 09-19-2009, 08:53 PM #7
Senior Member
- Join Date
- Oct 2008
- Posts
- 116
- Rep Power
- 0
basically I want when thread
class Advertisement extends Thread
{
public void run()....
exits alone
class TestAdvt
{
public static void main (String[] args)
{
get terminated, but when exit with advt.interrupt();
...
catch (InterruptedException e)
{
break;
...
continue running the threed from beginning ?Last edited by lse123; 09-19-2009 at 08:56 PM.
- 09-19-2009, 08:55 PM #8
Senior Member
- Join Date
- Oct 2008
- Posts
- 116
- Rep Power
- 0
you mean to transfer vars/methods between classes is the same as normal, in case of Thread classes ?
- 09-19-2009, 08:58 PM #9
Senior Member
- Join Date
- Oct 2008
- Posts
- 116
- Rep Power
- 0
must be different since now we have run(), what if a var get it's value from the run() ?
- 09-20-2009, 10:14 AM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
How2assign all instance vars of a subclass var to be same as a superclass var?
By gymshoe in forum New To JavaReplies: 6Last Post: 02-22-2009, 06:04 AM -
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
subclass vs inner class
By bugger in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:31 PM -
which class is superclass and subclass?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks