What is the difference between these two statements?

Asked

Viewed 137 times

1

I was reading some tutorials and documentations and came across two different ways to declare the RequestContext:

RequestContext requestContext = RequestContext.getCurrentInstance();
requestContext.execute(/*coisas*/);

and

RequestContext.getCurrentInstance().execute(/*coisas*/);

What I want to know is if there is any difference between these two, if yes, which ones? When should I use each of them?

  • 4

    In the first version, you save the instance in the variable requestContext. On Monday, if you need her again you’ll need to call getCurrentInstance again.

1 answer

2


In the first example, you’re taking the class instance RequestContext and inserting into a variable "local" (RequestContext requestContext) where its maintenance alters the status of the "Singleton" class (from the looks of it).

The second example is (RequestContext.getCurrentInstance().execute(/*coisas*/);) you would be taking the instance of the class itself and changing it, so if you need it in some local treatment, by doing RequestContext context = RequestContext.getCurrentInstance(); the instance will already be with the .execute() active.

Observe the codes:

Main Class.java

private static Singleton singleton;

public static void main(String[] args) {

    //Estou alterando a própria classe Singleton;
    Singleton.getSingleton().setAtivo(false);
    System.out.println(Singleton.getSingleton().isAtivo());//Imprime false

    //Aqui a variável singleton está com a instância modificada da classe Singleton;
    singleton = Singleton.getSingleton();
    singleton.setAtivo(true);
    System.out.println(singleton.isAtivo());//Imprime true

    //O que acontece já que a variável singleton e a própria instância Singleton estão compartilhando informação;
    Singleton.getSingleton().setAtivo(false);
    System.out.println(singleton.isAtivo());//Imprime false
    //Ou até:
    singleton.setAtivo(true);
    System.out.println(Singleton.getSingleton().isAtivo());//Imprime true
    //Imprime true pois como a classe Singleton foi instanciada uma única vez (no próprio construtor da classe), as informações acabam compartilhadas; 


}

Singleton class.java

    private boolean ativo;

    private static Singleton retorno = new Singleton();

    private Singleton(){
        this.setAtivo(false);
    }

    public static Singleton getSingleton(){
        return retorno;
    }

    public boolean isAtivo() {
        return ativo;
    }

    public void setAtivo(boolean ativo) {
        this.ativo = ativo;
    }

Note: I used the Singleton standard because that’s what the Requestcontext class seemed to me to be.

  • I don’t understand very well, when I assign to a variable I will use the same getCurrentInstance and when I call straight I create a new getCurrentInstance?

  • The getCurrentInstance() is used to return the only instance that exists of that class. For example, if you try to run RequestContext rc = new RequestContext();, will not be possible because the builder is private within the class itself, that is, only the class in question, has access. What the getCurrentInstance() does is return a class session RequestContext which is already in memory, and which is unique (because it is static), which was created when the "server" in this case was created.

  • So what you’re doing when you call it into another variable, is just creating a new reference to the instance that’s inside the class RequestContext, and change the new variable or itself RequestContext.getCurrentInstance(); it won’t make a difference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.