Cooperative Threads: Inner Class Problem

  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Class Threads
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
10 replies · 2K views
zak100
Messages
462
Reaction score
11
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 don't 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 errorsSomebody please guide me.Zulfi.
 
Physics news on Phys.org
Hi,
I have corrected the array problem but now i am getting static variable error:
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.
 
Yes. I read. Inner class is correct. There is no bracket problem.

Zulfi.
 
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.
 
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.
 
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.
 
My friend thanks for your non-trivial hints. I have solved this prob.

Zulfi.