Colegas,
Faz alguma diferença ou tem alguma vantangem em inicializar a variavel no momento da declaração ou no construtor?
Muito obrigado.
[code]
public class UserMB {
private UserFacade userFacade;
private RegionalFacade regionalFacade;
private UserVO userLogged;
public UserMB(){
this.userLogged = getUserLogged();
this.userFacade = new UserFacade();
this.regionalFacade = new RegionalFacade();
}
[/code]}
hiram
Junho 28, 2009, 1:16am
#2
This works well when the initialization value is available and the initialization can be put on one line. However, this form of initialization has limitations because of its simplicity. If initialization requires some logic (for example, error handling or a for loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.
http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html
Existe vantagem em inicializar na mesma linha, fora do construtor, se aquele valor for usado por todos os construtores.
Nesse caso, você inicializa uma vez só.