user7892809
user7892809

Reputation:

Java: Making classes with arrays and using those arrays in another class

I've looked around and haven't found an answer to exactly what I am asking so here it is:

I'm new to Java and I need to make a class that has 3 arrays which can be called from another class. I am lost because I don't know how to "properly" create the instance so that it can be called from the other class without inheritance.

public class Book {
    int[] Book0 = new int[7];
    int[] Book1 = new int[4];
    int[] Book2 = new int[3];

   public Book {
    Book0 = new int[] {1,2,3,4,5,6,7};
    Book1 = new int[] {};
    Book2 = new int[] {};
   }

}

 public class Catalogue {
   public Catalogue{
   }
}

In class Catalogue I'm not sure how to call the instance in class Book. Do I need to change class Book and put "this"? If so, is it this() or this.____? Or do you do that in class Catalogue?

Upvotes: 1

Views: 248

Answers (4)

Jorge Aranda
Jorge Aranda

Reputation: 66

I'm not sure, but if i understood you, you want do something like that:

public class Book {

    // private fields : Remmember, in java + OOP class members should be private
    private String[] page0;
    private String[] page1;
    private String[] page2;

   public Book(int sizeOfPage0, int sizeOfPage1, int sizeOfPage2) {
      page0 = new String[sizeOfPage0]; // Argument inside [] sets array size
      page1 = new String[sizeOfPage1];
      page2 = new String[sizeOfPage2];
   }

   // Getters

   public String[] getPage0() {
       return page0;
   }

   public String[] getPage1() {
       return page0;
   }

   public String[] getPage2() {
       return page2;
   }

}

public class Catalogue {

    private Book[] books;

    public Catalogue(Book[] books) {
        this.books = books;
    }

    public Book[] getBooks() {
        return this.books;
    }

}

public class MyApp {

    // entrypoint of the application
    public static void main(String[] args) {

         Book book1 = new Book(2, 1, 1);
         book.getPage0()[0] = "In some place of 'La mancha' which I do not want remember";
         book.getPage0()[1] = "lives some 'hidalgo' ... etc";
         book.getPage1()[0] = "Sancho Panza as squire of Don Quijote";

         Book book2 = new Book(1, 3, 1);

         // ... something similar...

         Book[] books = {book1, book2};

         Catalogue catalogue = new Catalogue(books);

         System.out.println(catalogue.getBooks()[0].getPage0()[0]);
    }

}

Remember to take each class into a separated file or get only one public class and the rest default per file

Upvotes: 0

Neo
Neo

Reputation: 3786

You would probably want to instantiate Book and use the instance.

In Catalogue class (or any other class that uses Book):

Book b = new Book();
b.Book0; // {1,2,3,4,5,6,7}
b.Book1; // {}
b.Book2; // {}

Also you should declare the arrays as explicitly public, to indicate everyone can see and modify them.

If this Book object will have further functionalities (methods it implements) you should probably not use it like this but declare the arrays as private, not allowing access to any other class, and using getters/setters instead (think if you really need direct access).

By the way, in your code you initialize the three arrays with empty arrays with sizes 3, 4, and 7. These initializations are overriden by the constructor (which resets the arrays).

Upvotes: 0

Sam Orozco
Sam Orozco

Reputation: 1258

You want to encapsulate your fields with getters and setters and give your getters public access type so you can access your array outside of the object, and package.

public class Book {
    int[] Book0;
    int[] Book1;
    int[] Book2;

   public Book {
    Book0 = {1,2,3,4,5,6,7};
    Book1 = new int[4];
    Book2 = new int[3];
   }


public int[] getBook0(){
  return Book0;
}

public int[] getBook1(){
  return Book1;
}

public int[] getBook2(){
  return Book2;
}



}

 public class Catalogue {
   public Catalogue{
     Book book = new Book();
     int[] tempArray = book.getBook0();
   }
}

Upvotes: 2

user2023608
user2023608

Reputation: 468

You need to create an object of the class and the you can use that object to call methods or a constructor.

Book book = new Book();

Upvotes: 0

Related Questions