i am pretty new to object-orientated Java. I am trying to add books into a "bookshelf", in order to print out the author, title and year of release of the book. The problem I am encountering is that the console prints out the values for the same book and not for both of the added ones.
public class Bookshelf {
public static void main(String[] args) {
registerBooks();
}
public static void registerBooks() {
Book b1 = new Book("Ikigai", "Ken Mogi", 2018);
Book b2 = new Book("The Alchemist", "Paulo Coelho", 1988);
System.out.println(getName(b1));
System.out.println(getName(b2));
}
static String getName(Book b) {
return Book.name;
}
static String getAuthor(Book b) {
return Book.author;
}
static int getYear(Book b) {
return Book.year;
}
}
public class Book {
static String name;
static String author;
static int year;
public Book(String bn, String ba, int be) {
name = bn;
author = ba;
year = be;
}
}
CodePudding user response:
In the getName()
, getAuthor()
... function replace Book.name
with b.name
. You try to access the static name
variable set in the Book class, which does not exist. As described in the comment by Arvind you should have a look at the term static
.
Static Variable access
Class.staticVariable
or Book.name
(Only if the variable has the term static)
Non Static Variable access
object.objectVariable
or ´book.name`
You have one "building plan" called book
, that has different properties like name
and author
. When actually creating the object with new Book(...)
the building plan is used to create a unique book model.
Static Variables are always written UPPERCASE
CodePudding user response:
You are using the class inside the class so you have to change the code and you have to remove the public
identifier.
You also have to use the getters and setters for setting and getting the values of the properties of the each object of the specific class.
You can also use the shadowing
concept inside the constructor for the better code.
class MyClass {
public static void main(String args[]) {
Book book1 = new Book("Harry Potter and the Goblet of Fire", "J.K. Rowling", 2010);
System.out.println(book1.getName());
}
}
class Book {
private String name;
private String author;
private int year;
public Book(String name, String author, int year) {
this.name = name;
this.author = author;
this.year = year;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setYear(int year) {
this.year = year;
}
public String getName() {
return this.name;
}
public String getAuthor() {
return this.author;
}
public int getYear() {
return this.year;
}
}
CodePudding user response:
You're making two mistakes here:
- You have static members in a class that should be instance members
- You're accessing the wrong variables in your get-Methods
Fix:
public class Bookshelf {
public static void main(String[] args) {
registerBooks();
}
public static void registerBooks() {
Book b1 = new Book("Ikigai", "Ken Mogi", 2018);
Book b2 = new Book("The Alchemist", "Paulo Coelho", 1988);
System.out.println(getName(b1));
System.out.println(getName(b2));
}
static String getName(Book b) {
return b.name;
}
static String getAuthor(Book b) {
return b.author;
}
static int getYear(Book b) {
return b.year;
}
}
and
public class Book {
public String name;
public String author;
public int year;
public Book(String bn, String ba, int be) {
name = bn;
author = ba;
year = be;
}
}
This should fix your code, although this is not an ideal implementation.
Note: In your code, you're essentially overwriting the values when you're creating the second book instance, because static members only exist once and are available throughout all instances. They're like global variables that belong to a specific class. When they are changed, they are changed for all instances.