Skip to content

Instantly share code, notes, and snippets.

@jayjaykim
Created August 21, 2016 12:44
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 jayjaykim/18d087a75662fc19afc93fe44dee58d9 to your computer and use it in GitHub Desktop.
Save jayjaykim/18d087a75662fc19afc93fe44dee58d9 to your computer and use it in GitHub Desktop.
java static keyword usage
public class TestStatic {
public static void main(String[] args) {
Sample sample1 = new Sample();
Sample sample2 = new Sample();
Sample sample3 = new Sample();
Sample.number1 = 2;
Sample.method1();
sample1.setNumber2(1);
sample2.setNumber2(3);
sample3.setNumber2(4);
System.out.println("number1 : " + Sample.number1);
System.out.println("sample 1 : " + sample1.getNumber1() + ", " + sample1);
System.out.println("sample 2 : " + sample2.getNumber1() + ", " + sample2);
System.out.println("sample 3 : " + sample3.getNumber1() + ", " + sample3);
}
}
public class Sample {
public static int number1 = -1;
int number2 = -1;
public void setNumber1(int n) {
this.number1 = n;
}
public int getNumber1() {
return number1;
}
public int getNumber2() {
return number2;
}
public void setNumber2(int number2) {
this.number2 = number2;
}
public static void method1() {
System.out.println("I'm static method");
}
@Override
public String toString() {
return "Sample{" +
"number2=" + number2 +
'}';
}
}
@jayjaykim
Copy link
Author

Sample.java, TestStatic.java 클래스를 각기 만들어 사용하세요.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment