Skip to content

Instantly share code, notes, and snippets.

@mohamad-amin
Last active March 15, 2017 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohamad-amin/3ad1f1cab0030c3644a9c244cafeda7b to your computer and use it in GitHub Desktop.
Save mohamad-amin/3ad1f1cab0030c3644a9c244cafeda7b to your computer and use it in GitHub Desktop.
A java sample about this issue in EasyMVP repository: https://github.com/6thsolution/EasyMVP/issues/28

As described here the static field is supposed to keep the value among all SimplePresenter classes and that's the default behaviour of it. The code above prints: 0 1 2 3 4 5 6 7 8 9

If you want it to be 0 0 ... 0 you should remove the static keyword so that it doesn't get shared among all presenters.

public class Main {
public static void main(String[] args) {
for (int i=0; i<10; i++) {
SimplePresenter presenter = new SimplePresenter();
}
}
}
/**
* Created by mohamadamin on 3/15/17.
*/
public class SimplePresenter {
private static int counter = 0;
private int count = counter++;
public SimplePresenter() {
System.out.println(count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment