public final class Math {
private Math() {}
public static final double E = 2.7182818284590452354;
public static final double PI = 3.14159265358979323846;
private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
private static final double RADIANS_TO_DEGREES = 57.29577951308232;
}
예시 ] Math 라이브러리를 이용하여 PI를 출력하는 코드
public class Hello {
public static void main(String[] args) {
System.out.println(Math.PI);
}
}
[Console]
3.141592653589793
public final class System {
private System() {
}
public static final PrintStream out = null;
}
public class PrintStream extends FilterOutputStream
implements Appendable, Closeable
{
public void print(boolean b) {
write(String.valueOf(b));
}
public void print(char c) {
write(String.valueOf(c));
}
public void print(int i) {
write(String.valueOf(i));
}
public void print(long l) {
write(String.valueOf(l));
}
public void print(float f) {
write(String.valueOf(f));
}
public void print(double d) {
write(String.valueOf(d));
}
public void print(char s[]) {
write(s);
}
public void println() {
newLine();
}
public void println(boolean x) {
synchronized (this) {
print(x);
newLine();
}
}
public void println(char x) {
synchronized (this) {
print(x);
newLine();
}
}
public void println(int x) {
synchronized (this) {
print(x);
newLine();
}
}
public void println(long x) {
synchronized (this) {
print(x);
newLine();
}
}
public void println(float x) {
synchronized (this) {
print(x);
newLine();
}
}
public void println(double x) {
synchronized (this) {
print(x);
newLine();
}
}
public void println(char x[]) {
synchronized (this) {
print(x);
newLine();
}
}
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
}
class NumberPrinter {
private int myNum = 0;
static void showInt(int n) {
System.out.println(n);
}
static void showDouble(double n) {
System.out.println(n);
}
void setMyNumber(int n) { myNum = n; }
void showMyNumber() { showInt(myNum); }
// 일반적인 함수도 클래스 함수(static 함수)를 호출할 수 있다.
}
public class ClassMethod {
public static void main(String[] args) {
NumberPrinter.showInt(20);
// static이 붙으면 class의 이름을 대고 바로 접근하여 호출할 수 있다.
NumberPrinter np = new NumberPrinter(); // 객체 생성
np.showDouble(3.15);
// 객체를 생성하여 static 함수에 접근하기
np.setMyNumber(75);
np.showMyNumber();
}
}
PrintStream out = new PrintStream();
out.print(text);
⨠
System.out.print(text);
class SimpleCalculator {
static final double PI = 3.1415; // 파이 값
static double add(double n1, double n2) {
return n1 + n2;
} // 더하기 함수
static double min(double n1, double n2) {
return n1 - n2;
} // 빼기 함수
static double calCircleArea(double r) {
return PI * r * r;
} // 원의 넓이 구하기 함수
static double calCirclePeri(double r) {
return PI * (r * 2);
} // 원의 둘레 구하기 함수
}
class Car {
int a;
static int b;
Car(){
System.out.println("Car 생성?");
}
void myCar() {
System.out.println("This is my car.");
}
public static void main(String[] args) {
myCar(); // 오류
Car car = new Car(); // 오류 X
System.out.println(a); // 오류
System.out.println(b); // 오류 X
}
}
class A {
static int a = 0;
static B b = new B(); // b 객체 생성
// static 없으면 main(static 함수)에서 오류 발생
}
class B {
void print() {
System.out.println("Test"); // b.print는 Test 출력
}
}
class ClassMethod {
public static void main(String[] args) {
System.out.println(A.a);
A.b.print();
}
}