0

The method definition LibraryBook is a void method, but it does not have void in the head of its definition. Why does it not cause error? I try another method definition and the other really causes error: invalid method declaration.

/**
/The following is a definition of the LibraryBook class.
/This class has four instance variables and one static (or class) variable.
/One of the instance variables is a type Author.
**/
public class LibraryBook
{
    private int bookId;
    private String bookName;
    private Author bookAuthor;
    private double bookPrice;
    private static double totalInvValue;


    public LibraryBook (int id, String name, Author author, double price)
    {
        bookId = id;
        bookName = name;
        bookAuthor = author;
        bookPrice = price;
        totalInvValue += price;
    }


    public String toString()
    {
        String response = "";
        response += "Book Name is: " + bookName;
        response += "\nIt costs: " + bookPrice;
        response += "\nIt was " + bookAuthor.toString();
        return response;
    }


    public static double getTotValue()
    {
        return totalInvValue;
    }

}
Hiep
  • 109
  • 1
  • 2
  • 6
  • 2
    Because `LibraryBook()` is a *Constructor* and Constructors don't return anything, not even *void*. It is different from a *normal* method. – TheLostMind Sep 29 '14 at 14:53

2 Answers2

1

You know it is a constructor, because it shares exactly the same name as the class itself.

Class-name = Constructor's name

It is not used as a method, because it isn't one.

It is used when instantiating an Object.

ChristopherMortensen
  • 1,093
  • 2
  • 8
  • 10
0

constructors are not methods they dont return anything

Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59