aspose file tools*
The moose likes Beginning Java and the fly likes inner question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inner question" Watch "inner question" New topic
Author

inner question

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
Inner class question:
Q.1 Given:
public class MyClass {
public class MyRunnable implements Runnable
{
public static void main(String [] args)
{
(new Thread(new MyRunner())).start();
}
}
How to rectify it? change it to (new Thread(new MyClass().new MyRunner())).start(); ??
Thanks
Andrew
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Andrew
A couple of points.
What is the actual question you're asking?
You can't have a static method in a non-static inner class, so you'll have to move your main method to the enclosing class.
To create an instance of the inner class you need to an instacne of the enclosing class first.
This code should do what you'r elooking for:

hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Sorry,
I missed the void run() method and they should be separated.
Here is what I ask:
public class MyClass
{
public static void main(String [] args)
{
//MyClass.MyRunnable mr = new MyClass().new MyRunnable();
//new Thread(mr).start();
(new Thread(new MyClass().new MyRunnable())).start(); //I want to know whether they are the same.
}
public class MyRunnable implements Runnable
{
public void run(){}
}
}
Thanks for your kind help.

Andrew
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Andrew
Yes both ways you have shown will accomplish the same thing - they both create and start a MyRunnable object. The major difference is that in the first way you'll actually have a reference to a MyRunnable object that you can refer to later in your code. The second way you wont.
hope that helps
------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Thanks a lot, Dave.
I got it.
Regards
Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner question