Junior
Čo znamená kľúčové slovo transient v Jave?
sobes.tech AI
Odpoveď od AI
Používa sa pri definovaní polí triedy a označuje, že toto pole by nemalo byť serializované počas štandardnej serializácie Java (použitím java.io.Serializable).
import java.io.Serializable;
public class User implements Serializable {
private String username;
private transient String password; // Toto pole nebude serializované
private int age;
// Konštruktor, gettery a settery
public User(String username, String password, int age) {
this.username = username;
this.password = password;
this.age = age;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public int getAge() {
return age;
}
}
Po deserializácii budú polia transient inicializované predvolenými hodnotami pre ich typ (0 pre číselné primitívne typy, false pre boolean, null pre referenčné typy).