Java JfileChooser doubt code

1

I understand that here I instantiate the class JFileChooser :

JFileChooser arquivo = new JFileChooser();

I get the object from the file and call the method of class JFileChooser (correct me if I'm wrong):

arquivo.setDialogTitle("Selecionar Arquivo");
arquivo.setFileSelectionMode(JFileChooser.FILES_ONLY);
int retorno = arquivo.showOpenDialog(this);

In the following excerpt I did not understand. I call the method getSelectedFile(); of class JFileChooser and then play it inside a class? I've never seen this happen in php.

What happened to arquivo.getSelectedFile(); ? I threw this inside where? File file is what ?? num is an incomplete class? Or is File a variable file of type File?

if(retorno == JFileChooser.APPROVE_OPTION) {

    File file = arquivo.getSelectedFile();

  jTextField5.setText(file.getPath());

}else {


}
    
asked by anonymous 11.11.2018 / 23:03

1 answer

1

The java language is a strongly typed language , that is, every declared variable needs a specified explicit type.

What is happening on this line is that the getSelectedFile() returns the selected object in the JFileChooser component, in a type named File , and you are assigning this returned object to the file variable of this same type. The fact that the variable name is file is only the code author's choice, it has nothing to do with its type, you could call arquivo also, but the variable would still be of type File .

There is nothing abnormal there, it is a return value assignment of a method to a variable, with difference that java requires that the variable declared on the left has to inform its type.

    
11.11.2018 / 23:11