Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active December 19, 2021 10:19
Show Gist options
  • Save sunmeat/f1bc73cfc5928cb4a7c6 to your computer and use it in GitHub Desktop.
Save sunmeat/f1bc73cfc5928cb4a7c6 to your computer and use it in GitHub Desktop.
package com.alex.static;
class Monster {
private static int count;
private int health;
private int attack;
public Monster() throws Exception {
if (count >= 1) {
throw new Exception("oops!");
}
count++;
System.out.println("monster #" + count);
}
public static int getCount() {
return count;
}
}
public class Program {
public static void main(String[] args) throws Exception {
// System.out.println(Monster.count);
// Monster.count = 5;
System.out.println(Monster.getCount());
Monster m = new Monster();
System.out.println(Monster.getCount());
Monster m2 = new Monster();
System.out.println(Monster.getCount());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment