๐ฅ ์ ๋ณด์ฒ๋ฆฌ ๊ธฐ์ฌ ์ค๊ธฐ ํ๋ก๊ทธ๋๋ฐ-์ฌ์ฉ์ ์ ์ ํจ์
public class Test {
public static void main(String[] args) {
System.out.print(Test.check(1));
}
"( 1 )" String check(int num){
return (num >= 0) ? "positive" : "negative";
}
}
[๊ฐ์ฒด๋ณ์].[๋ฉ์๋]
์ ๋ฐฉ์์ผ๋ก ์ ๊ทผํด์ผํ์ง๋ง,
static์ ์ด์ฉํ๋ค๋ฉด ๊ฐ์ฒด๋ณ์ ์์ด [ํด๋์ค์ด๋ฆ].[๋ฉ์๋]
๋ฐฉ์์ผ๋ก ์ ๊ทผํ๋๊ฒ์ด ๊ฐ๋ฅํ๋ค.์ฆ ๋ฉ์ธ ๋ฉ์๋๊ฐ ์๋๋๋ผ๋, static๋ฉ์๋ ์์์ ์ผ๋ฐ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ์ค๋ฅ๋ฅผ ์ผ๊ธฐํ๋ค.
int increment() {
static int x = 0;
x += 2;
return x;
}
int main() {
int x = 1;
int sum = 0;
for (int i = 0; i < 4; i++) {
x++;
sum += increment();
}
printf("%d", sum);
}
static int x
๋ "static" ์ผ๋ก ์ ์ธํ์ฌ ์ ์ญ๋ณ์(์ด๋๊ณณ์์๋ ์ฐธ์กฐ๊ฐ๋ฅํ ๋ณ์)๋ก ์ฌ์ฉ๋์ด ํธ์ถ(์ฌ์ฉ) ์ดํ์๋ ์ด๊ธฐํ ๋์ง์๋๋ค.def test(v):
if type(v) == type(""):
return len(v)
elif type(v) == type(100):
return 101
else:
return 20
a = "100.0"
b = 100.0
c = (100.0, 200.0)
print(test(a) + test(b) + test(c))
public class Main {
public static void main(String[] args) {
int sum = 0;
try {
func();
} catch(NullPointerException e) {
sum = sum + 1;
} catch(Exception e) {
sum = sum + 10;
} finally {
sum = sum + 100;
}
System.out.print(sum);
}
static void func() throws Exception {
throw new NullPointerException();
}
}
class B {
int x = 3;
int getX() {
return x * 2;
}
}
class A extends B {
int x = 7;
@Override
int getX() {
return x * 3;
}
}
public class Annotation {
public static void main(String[] args) {
B b1 = new A();
A b2 = new A();
System.out.print(b1.getX() + b1.x + b2.getX() + b2.x);
}
}
#include
void swap() {
int a = 11;
int b = 19;
int t = a;
a = b;
b = t;
}
int main() {
int a = 11;
int b = 19;
swap();
switch(a) {
case 1:
b += 1;
case 11:
b += 2;
default:
b += 3;
break;
}
printf("%d", a-b);
}
class Connection {
private static Connection _inst = null;
private int count = 0;
static public Connection get() {
if(_inst == null) {
_inst = new Connection();
return _inst;
}
return _inst;
}
public void count() {
count++;
}
public int getCount() {
return count;
}
}
public class main {
public static void main(String[] args) {
Connection conn1 = Connection.get();
conn1.count();
Connection conn2 = Connection.get();
conn2.count();
Connection conn3 = Connection.get();
conn3.count();
conn1.count();
System.out.print(conn1.getCount());
}
}