Cooperative Threads: Inner Class Problem

  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Class Threads
AI Thread Summary
The discussion revolves around a Java programming issue where the user is attempting to create an array of threads to perform addition on two arrays of integers. The initial code contains syntax errors related to the instantiation of a non-static inner class, which leads to compilation errors. The user receives guidance to review the rules for instantiating non-static inner classes and correct their array initialization. After addressing the array issue, the user encounters a static context error when trying to instantiate the inner class. Further advice is given to look into how to properly instantiate the inner class within the static context of the main method. Ultimately, the user resolves the problem after receiving hints about the correct syntax needed for instantiation.
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.
 
Technology 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.
 
Did you read the first link I provided?
 
Yes. I read. Inner class is correct. There is no bracket problem.

Zulfi.
 
And what are you trying to do in the line where the error occurs?
 
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.
 
Right. And what does the page say about instantiating non-static inner classes?
 
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.
 
  • #10
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.
 
  • #11
My friend thanks for your non-trivial hints. I have solved this prob.

Zulfi.
 

Similar threads

Back
Top