How to save a class instance in an ArrayList that is in the class? It has to be done in the constructor (Java)

Roy Karam

Given a class like the this:

public class Book {
    private String title;
    private int pages;
    private static ArrayList<Book> ArrayBooks = new ArrayList<Book>();

    public Book(String titl, int page){
        title=titl;
        pages=page;
    }
}

How can I keep a reference to every Book in the List, in a way that doesn't require any special calls - ie done automatically by the constructor.

Also, the List should have a max size of 20 books; if the user attempts to create the 21st book, he shouldn't be able to.

Can anyone figure out a way to implement it?

Tom

To access the current object, you could use the this reference. So your constructor could look like this:

public Book(String title, int pages) {
    this.title = title; // avoid abbreviations like "titl"
    this.pages = pages; // or "page" just to avoid same variable names
    ArrayBooks.add(this); // add new Book instance into the list
}

To avoid more than 20 entries, you have to check the current amount of entries in the list:

public Book(String title, int pages) {
    this.title = title;
    this.pages = pages;
    (if ArrayBooks.size < 20) {
        ArrayBooks.add(this); // add new Book instance into the list
    }
}

And I recommend to read the Java naming conventions. So ArrayBooks should be arrayBooks.

Edit

To prohibit more than 20 Book instances, you'll need a factory method that takes care about creating the instances for you:

public static Book create(final String title, final int pages) {
    if (ArrayBooks.size() < 20) {
        final Book instance = new Book(title, pages);
        ArrayBooks.add(instance);
        return instance;
    }
    return null;
}

private Book(String title, int pages) { // "hide" the constructor, so the user can't use this instead of the factory
    this.title = title;
    this.pages = pages;
}

You could also throw an exception in your constructor, but I personally don't like that. It is up to you, which way you take.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Php - making a class instance that has dependencies in the constructor

From Dev

Class Constructor - No Instance of Constructor

From Dev

How to save the instance of singleton class

From Dev

How to save state of instance class?

From Dev

How to: copying an instance of a class without a proper default constructor; that has members without a default constructor?

From Dev

How to refresh instance of class if some action in another class was done

From Dev

Java reflection create instance of unknown class and constructor

From Dev

Java creating instance of ArrayList from other class

From Dev

How to pass array to constructor and save in class variable

From Dev

How can I create an instance of class without invoking constructor of this class?

From Dev

How do I create an instance of a inner class in the outer class constructor

From Dev

Haskell :How to define an instance of the constructor class Foldable for the constructor

From Dev

create inner class instance in constructor

From Dev

Instance of abstract class with hidden constructor

From Dev

Passing parameterized Class instance to the constructor

From Dev

JAVA: How can I initialize an ArrayList to specific class type with Object-wrapped instance?

From Dev

Why can I not access an instance of class that is created in a constructor ( Java )

From Dev

Passing template class instance to a constructor of another class

From Dev

Repainting an instance of a class from an ArrayList

From Dev

How to use ArrayList while adding something to another Class's constructor ?

From Dev

class has no constructor forward declaration

From Dev

How to make a subclass constructor based on a parent class instance?

From Dev

How to create instance of subclass with constructor from super class

From Dev

How to create an object instance of class with internal constructor via reflection?

From Dev

How to initialise static instance of a class without copy constructor>

From Dev

Save an instance of a reference class in R

From Dev

Save an instance of a reference class in R

From Dev

java.lang.InstantiationException: class has no zero argument constructor

From Dev

How to concat Java string with .class in Intent constructor?

Related Related

  1. 1

    Php - making a class instance that has dependencies in the constructor

  2. 2

    Class Constructor - No Instance of Constructor

  3. 3

    How to save the instance of singleton class

  4. 4

    How to save state of instance class?

  5. 5

    How to: copying an instance of a class without a proper default constructor; that has members without a default constructor?

  6. 6

    How to refresh instance of class if some action in another class was done

  7. 7

    Java reflection create instance of unknown class and constructor

  8. 8

    Java creating instance of ArrayList from other class

  9. 9

    How to pass array to constructor and save in class variable

  10. 10

    How can I create an instance of class without invoking constructor of this class?

  11. 11

    How do I create an instance of a inner class in the outer class constructor

  12. 12

    Haskell :How to define an instance of the constructor class Foldable for the constructor

  13. 13

    create inner class instance in constructor

  14. 14

    Instance of abstract class with hidden constructor

  15. 15

    Passing parameterized Class instance to the constructor

  16. 16

    JAVA: How can I initialize an ArrayList to specific class type with Object-wrapped instance?

  17. 17

    Why can I not access an instance of class that is created in a constructor ( Java )

  18. 18

    Passing template class instance to a constructor of another class

  19. 19

    Repainting an instance of a class from an ArrayList

  20. 20

    How to use ArrayList while adding something to another Class's constructor ?

  21. 21

    class has no constructor forward declaration

  22. 22

    How to make a subclass constructor based on a parent class instance?

  23. 23

    How to create instance of subclass with constructor from super class

  24. 24

    How to create an object instance of class with internal constructor via reflection?

  25. 25

    How to initialise static instance of a class without copy constructor>

  26. 26

    Save an instance of a reference class in R

  27. 27

    Save an instance of a reference class in R

  28. 28

    java.lang.InstantiationException: class has no zero argument constructor

  29. 29

    How to concat Java string with .class in Intent constructor?

HotTag

Archive