Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No enclosing instance of type fbMain is accessible. Must qualify the allocation with an enclosing instance of type fbMain [duplicate]

Tags:

java

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.

like image 285
user7478 Avatar asked Mar 06 '12 21:03

user7478


1 Answers

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.

like image 165
Mark Peters Avatar answered Sep 24 '22 02:09

Mark Peters