Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 238 Big Data Appliance
- 1.9K Data Science
- 450.2K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 154 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 436 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Cooperative Threads: Inner Class Problem

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.
Best 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.
- The easier but worse oprtion is to add the key word static before the declaration of variable job.
- The better one is to move your logic into a new method and change the content ov main() to
new Question().theMethodWithTheLogic();
bye
TPD
Answers
-
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.
- The easier but worse oprtion is to add the key word static before the declaration of variable job.
- The better one is to move your logic into a new method and change the content ov main() to
new Question().theMethodWithTheLogic();
bye
TPD
-
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.
-
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.