Forum Stats

  • 3,770,449 Users
  • 2,253,116 Discussions
  • 7,875,459 Comments

Discussions

Cooperative Threads: Inner Class Problem

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Sep 20, 2016 2:07PM in New To Java

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]= i;

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

   input2[i]= 1;

job[ ] t = new job[20];

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

  job[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 in my code:

  job[i] = 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[i] = new job(i);

           ^

2 errors

Somebody please guide me.

Zulfi.

TPD-Opitzbudin

Best Answer

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Sep 20, 2016 5:57AM Accepted Answer

    The error message is quite clear and the problem has nothing to do with the inner class.

    Your business logic lives in the static method main().

    Your variable job holding the array is an Object member. this means you need to access it from a (non static) object member method.

    You have two options.

    1. The easier but worse oprtion is to add the key word static before the declaration of variable job.
    2. The better one is to move your logic into a new method and change the content ov main() to
      new Question().theMethodWithTheLogic();

    bye

    TPD

    budin

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Sep 20, 2016 5:57AM Accepted Answer

    The error message is quite clear and the problem has nothing to do with the inner class.

    Your business logic lives in the static method main().

    Your variable job holding the array is an Object member. this means you need to access it from a (non static) object member method.

    You have two options.

    1. The easier but worse oprtion is to add the key word static before the declaration of variable job.
    2. The better one is to move your logic into a new method and change the content ov main() to
      new Question().theMethodWithTheLogic();

    bye

    TPD

    budin
  • Unknown
    edited Sep 20, 2016 10:30AM
    job[ ] t = new job[20];. . .I am getting following error in my code: job[i] = new job(i);

    This IS NO VARIABLE name 'job' in what you posted.

    The variable is named 't' - so 'job[i]' has NO MEANING.

    TPD-Opitzbudin
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Sep 20, 2016 2:07PM

    Hi,

    Thanks for your guidance. I did it using both the methods:

    Method 1 (By making job class static)

    public class Question{

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

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

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

    static 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]= i;

    job[ ] t = new job[20];

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

      t[i] = new job(i);

      t[i].start();

    try{

      t[i].join();

    }catch(Exception e) {

    }

    }

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

       System.out.println(" " + output[i]);

    }

    }

    Method2 (Inner class job non-static, however works even if job is static)

    public class Question2{

    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

    new Question2().method2();

    }

    void method2( ) {

    int i=0;

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

       input1[i]= i;

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

       input2[i]= i;

    job[ ] t = new job[20];

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

      t[i] = new job(i);

      t[i].start();

    try{

      t[i].join();

    }catch(Exception e) {

    }

    }

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

       System.out.println(" " + output[i]);

    }//method2

    }//Question2

    Output:

    0

    2

    4

    6

    8

    10

    12

    14

    16

    18

    20

    22

    24

    26

    28

    30

    32

    34

    36

    38

    Thanks for helping.

    Zulfi.

This discussion has been closed.