Cooperative Threads: Inner Class Problem

In summary, you are trying to create an array of 20 threads to perform addition of 20 elements of two arrays. You have created an inner class. The inner class syntax is correct, but you are not accessing your arrays correctly.
  • #1
zak100
462
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
  • #3
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.
 
  • #4
Did you read the first link I provided?
 
  • #5
Yes. I read. Inner class is correct. There is no bracket problem.

Zulfi.
 
  • #6
And what are you trying to do in the line where the error occurs?
 
  • #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.
 
  • #8
Right. And what does the page say about instantiating non-static inner classes?
 
  • #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.
 
  • #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.
 

1. What is the Inner Class Problem in Cooperative Threads?

The Inner Class Problem in Cooperative Threads refers to the issue of accessing a non-static outer class variable from within an inner class. This is because inner classes have an implicit reference to the outer class, but this reference is not available to static methods. As a result, accessing an outer class variable from a static inner class method will result in an error.

2. How can the Inner Class Problem be solved in Cooperative Threads?

The Inner Class Problem can be solved by making the outer class variable static or by creating a new instance of the outer class within the inner class method. Alternatively, you can also use a static helper method that takes in the outer class instance as a parameter and returns the desired value.

3. What is the purpose of using inner classes in Cooperative Threads?

Inner classes in Cooperative Threads serve as a way to logically group classes within a single outer class. This can help improve code organization and encapsulation. They also have access to private members of the outer class, allowing for more efficient and secure code.

4. Are inner classes in Cooperative Threads always static?

No, inner classes in Cooperative Threads can be either static or non-static. Static inner classes are associated with the outer class itself, while non-static inner classes are associated with a specific instance of the outer class.

5. Can inner classes in Cooperative Threads access variables from other outer classes?

Yes, inner classes in Cooperative Threads can access variables from other outer classes as long as they have a reference to an instance of that outer class. This can be achieved by passing in the outer class instance as a parameter or by creating a new instance within the inner class method.

Similar threads

  • Programming and Computer Science
Replies
3
Views
772
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
2
Views
625
  • Programming and Computer Science
Replies
31
Views
2K
Back
Top