Cooperative Threads: Inner Class Problem

  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Class Threads
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to the implementation of an inner class in Java for creating an array of threads to perform addition on elements from two arrays. Participants explore syntax errors, static versus non-static context issues, and the correct instantiation of inner classes.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant presents a code snippet attempting to create an array of threads using an inner class and reports syntax errors related to variable referencing.
  • Another participant suggests reviewing resources on non-static inner classes and array access, indicating potential issues in the original code.
  • A participant acknowledges correcting the array access but encounters a static variable error, specifically related to instantiating the inner class.
  • Further discussion highlights the need to understand how to instantiate non-static inner classes correctly, with references to relevant educational resources.
  • A participant expresses frustration over the complexity of the topic but seeks clarification on the instantiation process.
  • Hints are provided regarding the necessary syntax for instantiating inner classes, leading to a resolution of the participant's issue.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the coding issues but do not reach a consensus on the best approach to resolve the inner class instantiation problem until later in the discussion.

Contextual Notes

Limitations include the potential misunderstanding of static versus non-static contexts in Java, as well as the specific requirements for instantiating inner classes, which remain unresolved in the initial exchanges.

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

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
20
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K