Created
August 21, 2016 12:44
-
-
Save jayjaykim/18d087a75662fc19afc93fe44dee58d9 to your computer and use it in GitHub Desktop.
java static keyword usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample.java, TestStatic.java 클래스를 각기 만들어 사용하세요.