Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Cooperative Threads: Inner Class Problem

  1. Sep 20, 2016 #1
    Hi,

    I am trying to create an array of 20 threads to perform addition of 20 elements of two arrays. I have created an inner class. I dont know if my inner class syntax is correct or not. My code is:


    public class Question{

    static int [ ] input1 = new int[20];

    static int [ ] input2 = new int[20];

    static int[ ] output= new int[20];



    class job extends Thread{

    private int index;

    job(int i) {

    index=i;

    }

    public void run() {

    output[index] = input1[index] + input2[index];

    }

    }



    public static void main(String[ ] args) {

    //initialize input arrays

    int i=0;

    for( i=0; i<20; ++i)

    input1= i;

    for( i=0; i<20; ++i)

    input2= 1;



    job[ ] t = new job[20];

    for(i=0; i<20;++i){

    job = new job(i);

    t.start();

    t.join();

    }



    for(i=0; i<20; ++i)

    System.out.println(" " + output);

    }

    }


    I am getting following error in my code:


    job = new job(i);

    ^

    symbol: variable job

    location: class Question

    Question.java:26: error: non-static variable this cannot be referenced from a st

    atic context

    job = new job(i);

    ^

    2 errors


    Somebody please guide me.


    Zulfi.
     
  2. jcsd
  3. Sep 20, 2016 #2

    Ibix

    User Avatar
    Science Advisor

  4. Sep 20, 2016 #3
    Hi,
    I have corrected the array problem but now i am getting static variable error:



    Code (Java):
    public class Question{
    static int [ ] input1 = new int[20];
    static int [ ] input2 = new int[20];
    static int[ ]  output= new int[20];
    class job extends Thread{
    private int index;
    job(int i) {
       index=i;
    }
    public void run() {
       output[index] = input1[index] + input2[index];
    }
    }
    public static void main(String[ ] args) {
    //initialize input arrays
    int i=0;
    for( i=0; i<20; ++i)
       input1[i]= i;
    for( i=0; i<20; ++i)
       input2[i]= 1;
    job[ ] t = new job[20];
    for(i=0; i<20;++i){
      t[i] = new job(i);
      t[i].start();
      t[i].join();
    }
    for(i=0; i<20; ++i)
       System.out.println(" " + output[i]);
    }
    }
     

    I am getting following error:

    Question.java:29: error: non-static variable this cannot be referenced from a static context
    t = new job(i);
    ^
    1 error

    Somebody please guide me.

    Zulfi.
     
  5. Sep 20, 2016 #4

    Ibix

    User Avatar
    Science Advisor

    Did you read the first link I provided?
     
  6. Sep 20, 2016 #5
    Yes. I read. Inner class is correct. There is no bracket problem.

    Zulfi.
     
  7. Sep 20, 2016 #6

    Ibix

    User Avatar
    Science Advisor

    And what are you trying to do in the line where the error occurs?
     
  8. Sep 20, 2016 #7
    Hi,
    In the line:
    t = new job(i);
    I am trying to initialize each element of array of object, job named 't', by invoking its constructor.

    Zulfi.
     
  9. Sep 20, 2016 #8

    Ibix

    User Avatar
    Science Advisor

    Right. And what does the page say about instantiating non-static inner classes?
     
  10. Sep 21, 2016 #9
    Hi,
    <instantiating non-static inner classes?>
    This is a lengthy topic.

    Reference https://www.physicsforums.com/threads/cooperative-threads-inner-class-problem.885979/
    <
    Inner Classes (Non-static Nested Classes)
    Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.

    Inner classes are of three types depending on how and where you define them. They are −

    • Inner Class
    • Method-local Inner Class
    • Anonymous Inner Class
    // Accessing he inner class from the method within . One example is
    void display_Inner() { Inner_Demo inner = new Inner_Demo(); inner.print(); }

    I am using main. So the above eg. does not fit.

    Do you know the answer?
    Plz don't make me to struggle so much. I am not asking a million dollar question.

    Zulfi.
     
  11. Sep 21, 2016 #10

    Ibix

    User Avatar
    Science Advisor

    But the error you are getting is when you try to instantiate the inner class. Scroll down a few paragraphs from the bit you've quoted to where it talks about how to instantiate the inner class.

    Hint: if there is example code creating a new object, what word must it use? You could search for that.
     
  12. Sep 22, 2016 #11
    My friend thanks for your non-trivial hints. I have solved this prob.

    Zulfi.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted