12388 (1) [Avatar] Offline

With Observable.just(), you cannot first create the Observable and then subscribe to it at a later point - the Observable creation and Observer subscription have to happen in the same block. The code has to look like this:
Observable.just(“string”) .subscribe(new Observer<String> ... );


How does the Observable know the subscription happened in the same block? For the observable, what is the difference between?

Observable.just(System.nanoTime()).subscribe(x -> System.out.println("Observer 1: " + x));


Observable<Long> justExample = Observable.just(System.nanoTime());
justExample.subscribe(x -> System.out.println("Observer 1: " + x));

The following code show subscribing multiple times to an Observable<Long> created using Observable.just(); I am using RxJava 1.2.6. The same behavior occurs with RxJava 2.0.6

public static void main(String[] args) throws InterruptedException {
    Observable<Long> first = Observable.just(System.currentTimeMillis());

    Observable.just(System.currentTimeMillis()).subscribe(x -> System.out.println("Immediate subscribe: " + x));

    first.subscribe(x -> System.out.println("Delayed subscribe: " + x));

    first.subscribe(x -> System.out.println("Subscribed after sleep: " + x));


Immediate subscribe: 1487608338720
Delayed subscribe: 1487608338688
Subscribed after sleep: 1487608338688

Doesn't the code block above show that Observable creation and Observer subscription can happen independently?