0

So my java code involves defining an object constructor class:

public static class bookID{
    bookID(String startAuthor, String startGenre, int startNumID){
        String author = startAuthor;
        String genre = startGenre;
        int numID = startNumID;
        String finalID = author + genre + Integer.toString(numID);
    }
}

And then instantiating the class after processing the user's input (in the main method of the program).

bookID newID = new bookID(authFinal, genre, numID); 
books.add(newID);
System.out.println(newID.finalID);

However, the last line is throwing an error, saying it 'cannot find symbol' of the finalID variable, despite newID already being called and it should have an instance of finalID as a field. Can anyone point out what I'm doing wrong here?

Here's the error:

Bookstore.java:100 error: cannot find symbol

System.out.println(newID.finalID);

symbol: variable finalID

location: variable newID of type bookID

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
znsolomon
  • 1
  • 1
  • 2
    That's because `finalID` is not a variable in the `bookID` class. It's a local variable in the `bookID` constructor. – azurefrog Oct 26 '17 at 19:49
  • [What is the difference between a local variable, an instance field, an input parameter, and a class field?](https://stackoverflow.com/q/20671008) – Pshemo Oct 26 '17 at 19:52

4 Answers4

0

finalID needs to be a global public variable to do that. Or private with a get method.

If you do the former, you can call newID.finalID. With the getter you can call newID.getFinalID();

Method 1:

public static class bookID{
    public String finalID;
    bookID(String startAuthor, String startGenre, int startNumID){
        String author = startAuthor;
        String genre = startGenre;
        int numID = startNumID;
        finalID = author + genre + Integer.toString(numID);
    }
}

Method 2:

 public static class bookID{
        private String finalID;
        bookID(String startAuthor, String startGenre, int startNumID){
            String author = startAuthor;
            String genre = startGenre;
            int numID = startNumID;
            finalID = author + genre + Integer.toString(numID);
        }

        getFinalID(){
            return finalID;
        }
    }
Tacitus86
  • 1,314
  • 2
  • 14
  • 36
0

First your fields should be global for the class and preferably declared private.

public class bookID{

    private String author;
    private String genre;
    private int numID;
    private String finalID;

Then overhaul constructor to look like this

bookID(String startAuthor, String startGenre, int startNumID){
    this.author = startAuthor;
    this.genre = startGenre;
    this.numID = startNumID;
    this.finalID = author + genre + Integer.toString(numID);
}

finally create getters for fields following the convention:

public String getAuthor(){
    return this.author;
}

also you can use the wrapper to avoid using Integer.toString() and just create a field of type Integer and then call method toString() on it

MiCkl
  • 63
  • 1
  • 6
0

In object oriented programming, member variables ("fields" in java) are not generally accessible outside the scope of the class (in some languages they are by default, but in java they are not unless declared public, which is usually not recommended).

If you want to be able to access these things, you need to make public "getter" or "setter" member functions within your class implementation to do so. For example, defining

public String getFinalID() {
    return finalID;
}

within your bookID class (also please follow naming conventions for Java unless you're instructed not to--this should be capitalized as BookID since it is a class) will allow you to read the value of finalID on any instance of the bookID class. Your code after processing user input would change to

bookID newID = new bookID(authFinal, genre, numID); 
books.add(newID);
System.out.println(newID.getFinalID());  //<-- notice the change here

This is done so that your public users do not have direct access to the internals of your classes unless you specifically allow it. In this case, by using a getter you do not allow the user to overwrite what is stored in finalID like they could if they had direct access by doing something like shown below:

bookID newID = new bookID(authFinal, genre, numID); 
books.add(newID);
newID.finalID = "something else";

But of course you can still accomplish this if you really want to by implementing a setter method:

public void setFinalID(newFinalID) {
    finalID = newFinalID;
}
Luke
  • 100
  • 9
0

There is a lot of trouble in your code.

class can't be static

Secondly, the name of your object don't represent it well. A good start could be :

public class Book {}

You're looking for to say that your Book can be defined by an startAuthor, String startGenre, int startNumID. For me, start has no sementic value, maybe there is for your need. But I would say :

public class Book {
     private String author;
     private String genre;
     private String id;

     public Book(String author, String genre, String id) {
            this.author=author;
            this.genre=genre;
            this.id=id;
     }

     public String getId(){ 
          return this.id; 
     }
     public String getAuthor(){ 
          return this.author; 
     }
     public String getGenre(){ 
          return this.genre; 
     }
}

The usage of getters and setters is a convention in Java This is encapsulation, keep it in mind, it's always the same when you understand the logic.

Now you can use it doint

Book book = new Book();
book.getId();

However, this lines that you wrote :

String author = startAuthor;

is totally different than

this.author=author;

In your case, author will be free at the end of the block instruction. I think you want to give it to the book attribute. this represents an instance of a book so this.author refer to the attribute of the Book while author without the this represent the closest defined attribute which is the parameter of the constructor.

Finally, if your finalID is a combination of your variables. I don't understand why you need to save it. it depends of what you need and how you need it, but if you want to just describe your object, this is enough.

@Override 
public String toString() {
    return author+genre+id;
} 
Djory Krache
  • 347
  • 4
  • 9