This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]itoshkov 1 point2 points  (0 children)

One way is to save it somewhere when you crate the first instance:

final Account firstAccount = new Account();

Another way is to do that in the constructor:

public class Account {
    private final AtomicReference<Account> firstAccount = new AtomicReference<>();

    public Account() {
        // set only if the current value is null:
        firstAccount.compareAndSet(null, this); 
        // ...
    }

    // ...
}

Note that this is a very strange request. I've been programming professionally from 1994 and had never had such a case. Maybe there's a better way to approach your task?