5

when trying to declare a new ObservableList:

ObservableList<Account> userAccounts = new FXCollections.observableArrayList();

I am getting an error at observableArrayList(); which says:

cannot find symbol, symbol: class observableArrayList, location: class FXCollections.

Here are my import statements

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

And here is my method

public ObservableList<Account> getUsersAccounts(int memberID) { 
    ObservableList<Account> userAccounts = new FXCollections.observableArrayList();

    try {     
        Statement stmt = conn.createStatement();            
        String sql = "SELECT * FROM account WHERE member_id='" + memberID + "'";            
        ResultSet rs = stmt.executeQuery(sql);

        while(rs.next()) {
            Account account = new Account(rs.getInt("member_id"), rs.getString("account_type"), rs.getDouble("balance"));
            userAccounts.add(account);
        }
    } catch (SQLException ex) {
        Logger.getLogger(JDBCManager.class.getName()).log(Level.SEVERE, null, ex);
    }

    return userAccounts;
}

What am I missing, why can't I declare a new ObservableList?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
fr33zex
  • 61
  • 1
  • 6
  • 6
    Get rid of the `new`. – Joe C Jan 30 '18 at 21:14
  • 1
    [`observableArrayList()`](https://docs.oracle.com/javase/9/docs/api/javafx/collections/FXCollections.html#observableArrayList--) is a static method, not a class. – James_D Jan 30 '18 at 21:17
  • that worked, but why? If i wanted to make an array list for example, ArrayList test = new ArrayList();, I would need the new – fr33zex Jan 30 '18 at 21:18
  • 1
    you don need to use `new` for a static methods so use: `ObservableList userAccounts = FXCollections.observableArrayList();` – JPRLCol Jan 30 '18 at 21:18
  • 1
    The `new` keyword is used when you call a constructor. – James_D Jan 30 '18 at 21:18
  • For a general discussion, read: [What are static factory methods?](https://stackoverflow.com/questions/929021/what-are-static-factory-methods) – jewelsea Jan 30 '18 at 22:59
  • @fr33zex, you've been provided 2 nice answers. Find a more useful one for you and accept it! [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Andrew Tobilko Jan 31 '18 at 07:41

2 Answers2

7

An instance can be created directly by using a constructor or implicitly by calling a method where this constructor can be invoked.

In your case, it's a static method. Have a look at these techniques:

List<String> a = new ArrayList<>();
List<String> b = Lists.createList();

class Lists {
    public static <T> List<T> createList() {
        return new ArrayList<>();
    }
}
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • 1
    I like this answer. This answer addresses the thing that the OP doesn't understand. – Jai Jan 31 '18 at 00:54
7

change

ObservableList<Account> userAccounts = new FXCollections.observableArrayList();

to

ObservableList<Account> userAccounts = FXCollections.observableArrayList();
parsa
  • 987
  • 2
  • 10
  • 23