简体   繁体   中英

error : cannot find symbol java for inner class

I am teaching myself socket programming using java , and I am encountering this problem while compiling . I have declared an inner class Book inside the main class library_s and the compiler is not recognizing the variable Status defined in the class Book . I am using Book as a static class because I read that enums only exist in static classes. I wanted an analog to "struct" in C because I am only familiar with that. I have looked at various other similar errors,, but none of them are helpful. Please help out. Thank you!

public class library_s extends Thread{

private ServerSocket lib_server;

LinkedList<Book> library = new LinkedList<Book>();

//constructor

  public library_s(int port) throws IOException{
    lib_server = new ServerSocket(port);
    lib_server.setSoTimeout(10000);
    }

public void run(){

Socket server = lib_server.accept();
    System.out.println("Connected to " + server.getRemoteSocketAddress());

    DataInputStream is = new DataInputStream(server.getInputStream());

    DataOutputStream os = new DataOutputStream(server.getOutputStream());

    for(library b : library){
      if(b.BookName == is.readUTF(){
           if(b.Status == FORISSUE){
         //enter rest of the body here
       }
      }

     }

   public static class Book{
    public String BookName;
    public static enum Status {FORISSUE, ISSUED, RENEW, RESERVE};

    public Book(String bn){
            this.BookName = bn;
            this.Status =   FORISSUE;
    }
}

public static void main(String [] args){
int port = Integer.parseInt(args[0]);
try{
    Thread t = new library_s(port);
    t.start();
} catch (IOException e){
    e.printStackTrace();
   }
  }
 }
}

The compiler gives error

library_s.java:64: error: cannot find symbol
    if(b.Status == FORISSUE){           
        ^
symbol:   variable Status
location: variable b of type Book
library_s.java:64: error: cannot find symbol
    if(b.Status == FORISSUE){           
                   ^

You declared the enum inside class Book so it's not recognized from outside (other classes). In order to fix it, either declare the enum inside library_s or outside Book the same way you would do with any other class in the package, or, if you want to keep that enum inside class Book you can access it using:

Book.Status.FORISSUE

You have declared a type Status , but not a variable of that type.

In your Book class:

private Status status;

public Book( ... )
{
    // ...
    this.status = Status.FORISSUE;
}

Outside of the class, you have to use Book.Status since it is a nested type. Eg.

if( b.status = Book.Status.FORISSUE ) { ... }

Status is a nested enum type inside Book class, not a field. You should be declaring a field of type Status . The assignment in your constructor is also suspicious. You are assigning a value of type Status to enum itself. That wouldn't compile.

Have a variable declared in your Book class as:

private Status status;

... and use it like:

if (b.status == Book.Status.FORISSUE)

Apart from that, you've numerous other compilation errors. Your if block as below is missing closing parenthesis:

if(b.BookName == is.readUTF()

Moreover, you're using == to compare string, which is not a compilation error, but functionally that will most certainly fail.

Another thing is, you don't need to declare your enum static . Nested enums are by default static only.

Lastly, please follow Java naming convention. It is so hard to read your code right now. Class name should start with UpperCase alphabets, and variable name with lowerCase alphabets.

I am using Book as a static class because I read that enums only exist in static classes.

Where did you read that? Please quote your sources. You can declare an enum in a non-static class too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM