Skip to content

Instantly share code, notes, and snippets.

@alexhudici
Forked from anonymous/java_Bj4kU6.java
Created February 17, 2017 17:25
Show Gist options
  • Save alexhudici/1b92479b4f52eaa741d4279949a9a705 to your computer and use it in GitHub Desktop.
Save alexhudici/1b92479b4f52eaa741d4279949a9a705 to your computer and use it in GitHub Desktop.
//*******************************************************************
// Edited with https://www.compilejava.net/
// Static member example
//*******************************************************************
import java.lang.Math; // headers MUST be above the first class
// one class needs to have a main() method
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
OtherClass myObject = new OtherClass();
System.out.println(myObject);
myObject.readCount();
OtherClass obj2 = new OtherClass();
obj2.readCount();
myObject.readCount();
}
}
// you can add other public classes to this editor in any order
public class OtherClass
{
private static int count; // gets 0 here automatically, but not if this was a local var of a method
public OtherClass(){
count++;
}
public void readCount(){
System.out.println(count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment