-3

i had a google for my issue and although i can find many questions with the same title, i cant seem to find a resolution that fits my example.

Library.java

public class Library {

public ArrayList<Book> books = new ArrayList<Book>();

public Library(){
      super();
    }

//Getters/Setters
public Library(ArrayList<Book> books) {
    this.books = books;
}

public ArrayList<Book> getBooks() {
    return books;
}

public void setBooks(ArrayList<Book> books) {
    this.books = books;
}

LibraryTester.java

import java.util.ArrayList;
import java.util.Scanner;

public class LibraryTester {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    ArrayList<Book> books = Library.getBooks(); //Getting error here
    books = Library.CreateBooksArrayList();
    MenuInput(sc, books);
    sc.close();
    Library.setBooks(books); //And here

}

Changing the getters/setters to static doesnt seem to work, im an absolute java noob so its probably some silly mistake, would anybody have any clue on how to access the getter/setter methods in the 'library' class from the 'library tester' class. Thanks in advance for any help

d1234
  • 101
  • 7
  • I would advise reading some [basic tutorial](https://docs.oracle.com/javase/tutorial/) –  Dec 10 '15 at 18:18
  • Possible duplicate of [calling non-static method in static method in Java](http://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java) –  Dec 10 '15 at 18:19
  • Class `Library` methods are **not** static, so you need an **instance** of the class to call them. – PM 77-1 Dec 10 '15 at 18:21
  • Java is a object oriented programming language. That means that you should to understand what is a class and how do you do an object of class etc. You should learn some basic first. – CyberAleks Dec 10 '15 at 18:28
  • I'm still getting the hang of working with objects after only having a light background in python, ill have a read of those help pages. thanks for the help and advice guys! – d1234 Dec 10 '15 at 18:32

2 Answers2

2

getBooks() is not a static method of Library class. You need to create an instance of a library then call the instance's method. (Also not sure what you're trying to accomplish with your main method when you query for the books list and then call setBooks to set it to the value you read...). Also would second the advice about reading a good tutorial or two.

import java.util.ArrayList;
import java.util.Scanner;

public class LibraryTester {

   public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Library lib = new Library();
    ArrayList<Book> books = lib.getBooks();
    MenuInput(sc, books);
    sc.close();
}
JJF
  • 2,681
  • 2
  • 18
  • 31
  • Ahhh, thank you! i see what you mean, i cant reference the class itself, i have to make an instance of that class in the tester class. Thank you very much JJF, have a nice night! – d1234 Dec 10 '15 at 18:30
1

Looks like you're attempting to test your code (good idea), but you're using the wrong tool for it. Don't wrap your test case in an application - instead use jUnit to execute and assert your class behavior. This might be useful.

How to write a Unit Test?

Community
  • 1
  • 1
  • Its for an assignment for my comp sci course, we haven't covered jUnit yet and the tester class is how we're meant to run it for now, i appreciate the comment though, ill definitely keep that in mind for future :) – d1234 Dec 10 '15 at 18:28