1. static
1) static 메소드
- 일반멤버메소드는 객체를 생성한 후에 사용할 수 있지만 static메소드는 객체생성없이 클래스명(**)으로 호출해서 사용한다.
- 일반멤버변수(인스턴스변수)는 사용할 수 없고 static멤버변수만 접근가능하다.
- 인스턴스변수를 사용하지 않는 독립적인 작업의 메소드를 만들때 static메소드를 사용한다.
- this는 사용할 수 없다.
- 만드는 방법 : 메소드명 앞에 static을 붙인다.
ex> static 메소드 사용예
class MyMath
{
private int sum;
public static int add(int x,int y)
{
//sum=x+y;
//오류발생:static멤버는 일반멤버변수에는 접근 불가!!
return x+y;
}
public static int max(int x,int y)
{
return (x>y)?x:y;
}
public static int abs(int x)
{
return (x>0)?x:-x;
}
}
class Test04_static
{
public static void main(String[] args)
{
//MyMath mm=new MyMath();
//int a=mm.add(1,2);
//static메소드는 객체는 new로 생성후 사용하는 것이 아니라
//객체생성없이 클래스명으로 호출한다!!!
int sum=MyMath.add(1,2);
System.out.println("두수합:"+sum);
int m=MyMath.max(1,2);
System.out.println("큰값:"+m);
System.out.println("4.5의 반올림값:"+ Math.round(4.5));
}
}
ex> 사각형의 넓이와 원의 넓이를 출력해 보세요.
class ShapeArea
{
public static final double PI=3.141592;
//사각형 넓이 구하는메소드
public static double rectArea(double x,double y)
{
return x*y;
}
//정사각형 넓이
public static double rectArea(double x)
{
return x*x;
}
//원의 넓이
public static double circleArea(double r)
{
return r*r*PI;
}
}
class Test05_static
{
public static void main(String[] args)
{
System.out.println("사각형넓이:"+ShapeArea.rectArea(1.3,2.5));
System.out.println("원의넓이:"+ShapeArea.circleArea(1.3));
System.out.println("PI:"+ShapeArea.PI);
System.out.println("PI:"+Math.PI);
System.out.println("int의 최대값:"+Integer.MAX_VALUE);
}
}
; 클래스명.static메소드 명으로 사용가능
; static변수, static 메소드 가능.
; Math.PI , Integer.MAX_VALUE는
2) static 멤버변수
- 인스턴스변수(일반멤버변수)는 객체의 수만큼 생성되지만 static멤버변수는 객체의 수와 상관없이 오로지 하나만 생성되어 모든 객체가 공유해서 사용한다.
- 만드는 방법: 멤버변수앞에 static을 붙여서 만든다.
- 일반멤버변수는 객체를 new로 생성하는 순간에 만들어지지만 static멤버변수(클래스멤버변수)는 new로 생성하지 않아도 클래스가 로딩되는 순간 생성된다.
ex> static member변수 사용예
class MyClass
{
private int a,b;
private static int c;
public MyClass()
{
a++;b++;c++;
}
public void printData()
{
System.out.println("a:"+a +",b:"+b+",c:"+c);
}
}
class Test06_static
{
public static void main(String[] args)
{
MyClass mc1=new MyClass();
mc1.printData();
MyClass mc2=new MyClass();
mc2.printData();
MyClass mc3=new MyClass();
mc3.printData();
}
}
ex> 총판매갯수를 static 변수를 사용
class Book
{
private String title; //도서명
private int cnt; //판매갯수
private static int totCnt; //총판매갯수
public Book(String title,int cnt)
{
this.title=title;
this.cnt=cnt;
totCnt+=cnt;
}
public void printInfo()
{
System.out.println("도서명:"+title+",판매갯수:"+cnt);
}
public static int getTotCnt()
{
return totCnt;
}
}
class Test07_static
{
public static void main(String[] args)
{
Book b1=new Book("Java",10);
b1.printInfo();
System.out.println("총판매갯수:"+Book.getTotCnt());
Book b2=new Book("Jsp",5);
b2.printInfo();
System.out.println("총판매갯수:"+Book.getTotCnt());
}
}
3) static초기화
ex> static초기화
class MySong
{
private int a;
private static String str;
//static멤버를 초기화할때는 static블록을 사용한다.
static{
str="아리랑 아리랑 아라리요";
}
public MySong(){
a=1000;
}
public void printA(){
System.out.println("노래가격:"+a);
}
public static void printStr(){
System.out.println("노랫가사:"+str);
}
}
class Test08_static
{
public static void main(String[] args)
{
MySong.printStr();
}
}
'JAVA' 카테고리의 다른 글
this (0) | 2014.09.12 |
---|---|
객체배열 (0) | 2014.09.12 |
final (0) | 2014.09.12 |
Overloading (0) | 2014.09.12 |
클래스( class ) (0) | 2014.09.12 |