Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: can't create the instance of the second class

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default can't create the instance of the second class

    Hi again! Can't get why the Eclipse underlines the line below
    msg error: No enclosing instance of type HelloRunnable is accessible. Must qualify the allocation with an enclosing instance of type HelloRunnable (e.g. x.new A() where x is an instance of HelloRunnable).

    public class HelloRunnable implements Runnable {
     
    	//run method for HelloRunnable i.e. for the thread #1
    	public void run(){
    		System.out.print("thread #1: ");
    		for(int i=0; i<10; i++){
    			System.out.print(i+" ");
    		}
    		System.out.println();
    	}
     
     
    	public class HelloRunnable2 implements Runnable {
     
    		// run method for HelloRunnable2 i.e. for the thread #2
    		public void run() {
    			System.out.print("thread #2: ");
    			for (int i = 10; i < 20; i++) {
    				System.out.print(i + " ");
    			}
     
    		}
     
    	}
     
     
     
      public static void main(String[] args) {
     
    	  (new Thread(new HelloRunnable())).start();
    	    (new Thread([COLOR="Red"]new HelloRunnable2()[/COLOR])).start();
     
    	}
     
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: can't create the instance of the second class

    HelloWorld2 class needs to be static.
    Also, I THINK declaring a public class more than once goes against convention? I can't be sure
    i.e. use class or private class instead for inner classes.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can't create the instance of the second class

    Hi to everyone, actually i wanna know why we have to use Threads and in this case what the guy above has posted what is purpose of that code since we can output that he wants to output just with writting this code in main class:

    System.out.print("thread #1: ");
    for(int i=0; i<10; i++){
    System.out.print(i+" ");
    }
    System.out.println();


    System.out.print("thread #2: ");
    for (int i = 10; i < 20; i++) {
    System.out.print(i + " ");


    ---------------------------------------------------------------------------------------------
    public class HelloRunnable implements Runnable {

    //run method for HelloRunnable i.e. for the thread #1
    public void run(){
    System.out.print("thread #1: ");
    for(int i=0; i<10; i++){
    System.out.print(i+" ");
    }
    System.out.println();
    }


    public class HelloRunnable2 implements Runnable {

    // run method for HelloRunnable2 i.e. for the thread #2
    public void run() {
    System.out.print("thread #2: ");
    for (int i = 10; i < 20; i++) {
    System.out.print(i + " ");
    }

    }

    }



    public static void main(String[] args) {

    (new Thread(new HelloRunnable())).start();
    (new Thread(new HelloRunnable2())).start();

    }

    }




    what is actually the difference doing this with thread and doing without threads since the result is the same?
    Anyone who can explain it ?

Similar Threads

  1. Compare instance of a class to another
    By srs in forum Java Theory & Questions
    Replies: 5
    Last Post: December 2nd, 2010, 10:39 AM
  2. Replies: 0
    Last Post: December 1st, 2010, 06:10 AM
  3. How to create a new object of another class within a method
    By davie in forum Object Oriented Programming
    Replies: 1
    Last Post: April 16th, 2010, 05:53 PM
  4. Create a java small class for a bank account!!?
    By jon123 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 24th, 2010, 11:00 AM
  5. class constants instance constants..... etc
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2009, 03:38 PM