public class Car {
int gas;
void setSpeed(int speed) {
}
}
위의 경우 Car클래스에 인스턴스 필드 gas, 인스턴스 메서드 setSpeed()를 선언하였다. 이 인스턴스 멤버는 외부클래스에서 사용하기 위해서
Car myCar = new Car();
myCar.gas = 10;
myCar.setSpeed(60);
Car yourCar = new Car();
yourCar.gas = 20;
yourCar.setPeed(80);
와 같이 객체를 생성하고 참조변수를 통해 접근해야한다.
public class 클래스 {
static 타입 필드 = 초기값;
static 리턴 타입
메소드(매개변수선언 ..) {
}
}
static멤버를 사용하려면 클래스이름.필드/클래스이름.메소드 와 같은 형식으로 접근한다.
public class Calculator {
static double pi = 3.14159;
static int plus(int x, int y) {
}
static int minus(int x, int y) {
}
}
와 같은 클래스에서는
double result1 = 10 * 10 * Calculator.pi;
int result2 = Calculator.plus(10,5);
int result3 = Calculator.minus(10,5);
와 같이 사용할 수 있다.
원칙적으로는 클래스이름으로 접근해야하지만 객체참조 변수로도 접근 가능하다.
Calculator mycal = new Calculator();
double result1 = 10 * 10 * mycal.pi;
int result2 = mycal.plus(10,5);
int result3 = mycal.minus(10,5);
즉,
class Calculator{
static double pi = 3.14159;
static int plus(int x, int y) {
return x + y;
}
static int minus(int x, int y){
return x - y;
}
}
public class CalEx {
public static void main(String[] args) {
double result1 = 10 * 10 * Calculator.pi;
int result2 = Calculator.plus(10,5);
int result3 = Calculator.minus(10,5);
System.out.println("result1: " + result1);
System.out.println("result2: " + result2);
System.out.println("result3: " + result3);
}
}
class Cal{
public static int abs(int a) {
return a > 0 ? a : -a;
}
public static int max(int a, int b) {
return (a > b) ? a : b;
}
public static int min(int a, int b){
return (a>b) ? b : a;
}
}
public class Calex {
public static void main(String[] args) {
System.out.println(Cal.abs(-5));
System.out.println(Cal.max(10,8));
System.out.println(Cal.min(-3,8));
}
}
static 메서드 내부에는 인스턴스 필드, 인스턴스 메서드를 사용할 수 없고, 참조변수 this도 사용이 불가능하다.
인스턴스 멤버를 사용하려면 객체를 생성하고 참조변수로 접근하면 된다.
public class ClassName {
//인스턴스 필드와 메서드
int field1;
void method1() {
}
//static필드와 메서드
static int field2;
static void method2() {
}
//정적 메서드
static void Method3(){
// this.field1 = 10; //컴파일 에러
// this.method1(); //컴파일 에러
ClassName obj = new ClassName();
obj.field1 = 10;
obj.method1();
field2= 10;
method2();
}
}
main메소드도 static메소드이므로 객체 생성없이 사용할 수 없다. 즉
public class Car {
int speed;
void run(){}
public static void main(String[] args) {
// speed = 60; //에러
// run(); //에러
Car mycar = new Car();
mycar.speed = 60;
mycar.run();
}
}
class StaticSample {
public int n;
public void g() {
m = 20;
}
public void h() {
m = 30;
}
public static int m;
public static void f() {
m = 5;
}
}
public class StaticEx {
public static void main(String[] args) {
StaticSample s1, s2;
s1 = new StaticSample();
s1.n = 5;
s1.g();
s1.m = 50;
s2 = new StaticSample();
s2.n = 8;
s2.h();
s2.f();
System.out.println(s1.m);
}
}
final class Mebmber{}
//class ImportantMember extends Mebmber{} //상속 불가
class Car{
public int speed;
public void speedUp() {
speed += 1;
}
public final void stop(){
System.out.println("멈춤");
speed = 0;
}
}
class SportsCar extends Car{
public void speedUp() {
speed += 10;
}
// public void stop(){ //재정의 불가
// System.out.println("스포추카 멈춤");
// speed = 0;
// }
}
class Person{
final String nation = "korea"; //필드선언할 떄 초기값 지정
final String ssn;
String name;
public Person(String ssn, String name){
this.ssn = ssn;
this.name = name;
}
}
public class PersonEx {
public static void main(String[] args) {
Person p1 = new Person("123456-1234567","홍길동");
System.out.println(p1.name);
System.out.println(p1.ssn);
System.out.println(p1.nation);
}
}
abstract class AbstractaTest{
abstract void move();
}
AbstractTest a = new AbstractTest(); 불가!
추상 메서드, 추상 클래스는 뒤에서 자세히 다룹니다.