How to assign a static makeMassClient method to the static clients field?

 class GPR extends JFrame implements MouseListener, ListSelectionListener { static Client[] clients=BankingSystem.Parser.makeMassClient("D://clients.out"); 

The problem is that the syntax error "Unhandled exception type IOException" appears to the right of the equal.

Multiple markers at this line
Unhandled exception type IOException
Unhandled exception type IOException

  • Put this construct inside a try-catch block that catches IOException - iksuy
  • @iksuy and is it possible to do tre-catch in the class fields? After all, a new syntax error appears: Syntax error on token ";", {expected after this token - Vladislav Solopov
  • Actually, what I showed you in the answer was what I meant - try-catch inside a static initialization block. - iksuy

1 answer 1

Use, for example, a static initialization block:

 static Client[] clients = null; static { try { clients = BankingSystem.Parser.makeMassClient("D://clients.out"); } catch(IOException e) { // Обработка исключения... } } 
  • Is it possible to make an assignment directly? Because I need to use this field in another class, and it is initially initialized as null. As a result, I can not get the returned data from the method - Vladislav Solopov
  • The execution of static initialization blocks is performed during class initialization in the order they are declared. Did not quite understand what your problem is - Pavel Parshin
  • @Parshi was another little bug in the code. Fixed, it works! Thank! - Vladislav Solopov