11

So in my class declared as "public class pcb", I have the following constructor: public pcb(int p, int a, int b).

In public static void main(String[] args) I try to call the constructor in a for loop where I add a "pcb" into a different position in an array. Here is the for loop where the last line is where I get the error:

for(int i=0; i<numJob; i++){
   pI = scan.nextInt();
   arr = scan.nextInt();
   bst = scan.nextInt();
   notHere[i]=new pcb(pI, arr, bst);
}

What am I doing wrong? Is it syntax or is it the structure of my program. I haven't used Java that much and I think that's my main problem.

user7478
  • 121
  • 1
  • 1
  • 4

2 Answers2

21

You haven't given all of the relevant code, but the error indicates that pcb is an inner class of fbMain:

public class fbMain {
    //...
    public class pcb {
    }
    //...
}

You can fix this error by making pcb static:

 public static class pcb {
 }

Or by moving the class to its own file. Or, if pcb cannot be static (because it is associated with an instance of fbMain), you can create a new pcb by passing an instance of fbMain:

notHere[i] = instanceOfFbMain.new pcb(pI, arr, bst);

It is likely the first that you want. Please also note that by convention, Java type names start with an upper-case letter.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
3

Add static to your class declaration like this

public static class pcb...
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117