Results 1 to 7 of 7
- 03-22-2014, 10:22 AM #1
Senior Member
- Join Date
- Mar 2014
- Posts
- 286
- Rep Power
- 7
Error - creating an instance of an inner class
Java Code:package Threads; // THIS PROGRAM WILL HAVE TWO THREADS i.e. "main" AND ANOTHER THREAD (SYSTEM WILL NAME IT "Thread-0" //THE STORY IS THAT WE WILL START Thread-0 FROM main AND LET IT EXECUTE. //main WILL WAIT AND LET IT EXECUTE FOR 5 MINUTES. //IF IT FINISHES ITS EXECUTION BEFORE 5 MINUTES, WELL AND GOOD; //BUT IF IT DOESN'T, WE WILL INTERRUPT IT. //AFTER INTERRUPTION, WE WILL DECIDE TO WAIT INDEFINITELY. public class SimpleThreadsCopy { public static void threadMessage(String s){ String sThreadName= Thread.currentThread().getName(); System.out.format("%s: %s%n", sThreadName, s); } public class MessageLoop implements Runnable{ public void run(){ String foodChain[]= {"Mares eat oats", "Doves eat oats", "Little lambs eat ivy", "A kid will eat ivy too" }; try{for (int i=0; i<foodChain.length; i++){ threadMessage(foodChain[i]); Thread.sleep(6000); } }catch(Exception e){ threadMessage("I have been interrupted."); } } } public static void main(String [] args){ threadMessage("STARTING THE THREAD NAMED MessageLoop!");//Announces that we are going to start the MessageLoop thead long timeTaken= 1000 * 60 * 5; long startTime = System.currentTimeMillis(); Thread t= new Thread(new MessageLoop()); //**************************************************ERROR t.start(); threadMessage("Waiting for MessageLoop to finish execution!"); while(t.isAlive()){ threadMessage("Still waiting...");//just says that the main thread is waiting for MessageLoop to complete, till 5 minutes try{t.join(1000);}catch(Exception e){threadMessage("t.join(1000); statement interrupted.");} if(System.currentTimeMillis() - startTime > timeTaken && t.isAlive()){ t.interrupt(); try{t.join();}catch(Exception e){threadMessage("t.join(); statement interrupted.");} // waits indefinitely } } } }
No enclosing instance of type SimpleThreadsCopy is accessible. Must qualify the allocation with an enclosing instance of type SimpleThreadsCopy (e.g. x.new A() where x is an instance of SimpleThreadsCopy).
Now that a similar "error-free" code is given here, what's wrong with this piece of code and what should I do about it?
EDIT:- Trying to understand the error statement, I replaced the erroneous statement withJava Code:Thread t= new Thread(new SimpleThreadsCopy().new MessageLoop());
But then why doesn't the code in the tutorial give an error?Last edited by Zarah; 03-22-2014 at 10:36 AM.
- 03-22-2014, 06:20 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Error - creating an instance of an inner class
I think the example defined MessageLoop as a statically nested class ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 03-22-2014, 06:41 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Error - creating an instance of an inner class
Here's a recommendation. When you enter main, which is a static method, you can have problems doing certain things
related to instances of the enclosing class. As such, I very seldom do anything in main except something like the following:
Assume a class named MyClass.
Java Code:public static void main(String[] args) { new MyClass().start(); } public void start() { // put here what you would // normally put in main }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-22-2014, 07:42 PM #4
Member
- Join Date
- Mar 2014
- Posts
- 1
- Rep Power
- 0
Re: Error - creating an instance of an inner class
its quite easy. you need not to use the key word static in the method "threadMessage" Bcz u r creating an object for class "SimpleThreadsCopy" so you need not put static
- 03-22-2014, 08:35 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Error - creating an instance of an inner class
That has nothing to do with the OP's problem and introduces even more error in the code. And just because one creates instances of classes does not mean those classes should not contain static methods. After all, main is a static method.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-24-2014, 06:28 AM #6
Senior Member
- Join Date
- Mar 2014
- Posts
- 286
- Rep Power
- 7
- 03-24-2014, 06:54 AM #7
Senior Member
- Join Date
- Mar 2014
- Posts
- 286
- Rep Power
- 7
Re: Error - creating an instance of an inner class
Similar Threads
-
Call a String from a Java class to another class without creating an instance
By kamlesh_waghela2000 in forum New To JavaReplies: 2Last Post: 09-16-2013, 05:19 PM -
Creating an instance variable in one class that connects to another instance variable
By SpicyElectricity in forum New To JavaReplies: 1Last Post: 04-21-2012, 07:03 PM -
error in creating the entity class by netbeans
By javastuden in forum NetBeansReplies: 2Last Post: 07-22-2011, 02:29 PM -
Finding and Creating Instance of Every Class in Package
By Julian Trust in forum New To JavaReplies: 5Last Post: 06-25-2011, 09:34 AM -
Help with creating an instance of a class extension
By Inferno719 in forum New To JavaReplies: 19Last Post: 05-04-2011, 04:53 AM
Bookmarks