1

According to a lecture on Coursera, classes are types and if we have a class called Book, for example, we can write a line of code-

Book book = new Book("Java",100);

which would create some data in the heap that we call an object and the variable 'book' of the type "Book" points to that object. Now my question is, that what kind of values can the variable 'book' have? I tried printing it using

System.out.println(book);

and it printed this -

eclipsetest.Book@24d46ca6

('eclipsetest' is the name of my package and 'Book' is the class) So what exactly is happening here?

EDIT: Not a duplicate of this because I know how to print the data using an object without getting that gibberish but I cannot understand what type of data classes as data types can hold. I also tried looking it up further and found that classes are 'reference' data types and hold the 'reference' to an object. How they do it, however, I don't understand since Java does not have any way to print the memory address like C.

WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
AviusX
  • 374
  • 2
  • 12
  • 2
    Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – that other guy Sep 20 '19 at 23:37
  • @Carcigenicate Edited the question for better understanding. I basically mean, that since a variable defined with int as a type can store integer values and such, what type of data can a variable with class as a type store? – AviusX Sep 20 '19 at 23:50
  • 2
    A class can hold any type, including itself. If you understand the 'gibberish' it is irrelevant to your question and should be removed. – user207421 Sep 21 '19 at 00:12
  • Don't edit your question to put an answer in it, put the answer as an answer – WhatsThePoint Feb 12 '23 at 16:06

2 Answers2

3

Indeed, classes are types and classes can hold instances of other types within them as part of their definitions. Such user-defined types can hold other user-defined types (classes, enumerations) or built-in types (int, boolean).

Given the example constructor you posted, that particular class might be defined like so:

public class Book
{
    private String title;
    private int numberOfPages;

    public Book(String title, int numberOfPages)
    {
        this.title = title;
        this.numberOfPages = numberOfPages;
    }
}

which holds the user-defined type of String (a class that's been defined for you as part of the standard libraries) and a built-in type of int.

In your attempt to get at the contents of the object that's been declared, you're trying to print it. However, your print statement is writing information about the reference, book, to the output, instead of going to the location of the actual object (what the reference is pointing to) and printing information about that -- there's no way to do this automatically in Java.

Instead, you will need to override the built-in java.lang.toString() method, as @bkbb alluded to, in order to get actual information stored in the object. When trying to print, you will either need to call this method explicitly

System.out.println(book.toString());

or make it be called automatically as part of a string operation

System.out.println("My book is: " + book);
paul
  • 1,655
  • 11
  • 23
0

All classes in Java are directly or indirectly derived from java.lang.Object class. You may want to learn more about hashCode(), toString(), equals() methods too and then think about your original post.

bkbb
  • 237
  • 1
  • 6