23.01.06(Java)

MH S·2023년 1월 6일

Java

목록 보기
3/16

1. static, final, 싱글톤(패턴)

-final: 클래스(상속 불가), 변수(상수화 단 선언과 동시에 초기화 필요없음), 메소드(오버라이딩 불가 - 상위 클래스에서 정의한 메소드를 하위 클래스 다시 재정의)

package ch06;

/*final(마지막) : 클래스, 필드(인스턴스 변수), 메소드
 * 1.클래스 : sub 클래스가 없는 클래스
 * 2.변수 : 상수선언(필드, 매겨변수, 지역변수)
 * 3.메소드 오버라이딩 불가
 * etc 오버라이딩: 상위클래스에서 정의한 메소드를 하위 클래스 다시 재정의.
 */

final class Final1{	
}
//class Final2 extends Final1{}

class Final3{
	static final int KIA=1;
	final int SK=2;
	int k;
	
	public Final3() {
		//KIA=2;
		System.out.println(k);
	}
	public void prn(final int a){
		//a=10;
		final int b=10;//지역변수(local variable)
		//b=20;
		int c;
		//System.out.println(c);
		int d=10;
		int e;
		e=10;
	}
}

class Final4{
	void prn1() {}
	final void prn2() {}
}

class Final5 extends Final4{
	@Override
	void prn1() {}
	
	//@Override
	//void prn2() {}
	
}

public class FinalEx1 {
	public static void main(String[] args) {
		
	}
}

-static: 필드, 메소드, 클래스(부분)
static 필드 및 메소드는 객체를 생성하지 않아도 사용가능.
사용시점: 클래스 이름으로 접근하기 때문에 그때 메모리에 로딩.

package ch06;

/*static: 필드, 메소드, 클래스(부분)
 * 
 * static 필드 및 메소드는 객체를 생성하지 않아도 사용가능.
 * 사용시점: 클래스 이름으로 접근하기 때문에 그때 메모리에 로딩.
 */

class Static1{
	static int i=0;
	int j=0;
	
	static void prn() {}
	void prn2() {}
	
	static class Inner {}//내부클래스 안에는 static 올수 있음.
	
}

public class StaticEx1 {
	public static void main(String[] args) {
		int a=Math.abs(-6);
		int b=(int) Math.round(3.14);
		
		Integer i=new Integer(22);
		int c=i.parseInt("23");
		int d=Integer.parseInt("23");
		//반지름이 5인 원의 넓이를 구하시오.
		System.out.println(5*5*Math.PI);
		
	}
}
package ch06;
/*static 필드 및 메소드는 클래스명.필드 or 메소드 일때
 * 메모리 로딩이 되지만 non-static 필드 및 메소드는
 * 반드시 객체를 생성해야 메모리 로딩된다.
 * 그래서 non-static 필드 및 메소드는 static 필드 및 메소드에
 * 사용 할 수 없다.
 * */

public class StaticEx2 {
	static int a=10;
	int b=10;//non-static
	
	static void prn1() {
		System.out.println(a);
		//static 메소드에는 non-static 사용불가
		System.out.println(b);
	}
	
	void prn2() {
		System.out.println(a);
		System.out.println(b);
	}
	
	public static void main(String[] args) {
		StaticEx2 st=new StaticEx2();
		st.prn1();
	}
}
package ch06;

class Static3{
	/*static 필드는 동일한 클래스의 모든 객체가 공유의 목적으로 하나만 메모리에 만들어진다*/
	static int a;
	int b;
	
}

public class StaticEx3 {
	public static void main(String[] args) {
		Static3 st1=new Static3();
		Static3 st2=new Static3();
		st1.b=10;
		st2.b=20;
		System.out.println(st1.b);
		System.out.println(st2.b);
		
		st1.a=10;
		st2.a=20;
		System.out.println(st1.a);
		System.out.println(st2.a);
	}
}

api 추가하는 법

Eclipse 에서 javadoc 한글 문제 처리 해결법

위의 api추가하는 과정을 통해 오류가 발생 할 경우
-encoding UTF-8 -charset UTF-8 -docencoding UTF-8 입력 후 Finish 버튼을 눌러 해결한다.


api 추가 완료시 StringUtil 클래스에 있는 addComma() 메소드를 통해 숫자의 단위를 세자리씩 끊어서 숫자를 간결히 확인할 수 있음.

이 밖에 각종 클래스안에 있는 다양한 메소드 호출을 통해 여러 기능들을 간단히 사용할 수 있음.

2. 접근지정자

-private
-protected: 같은 package 아닌 경우는 반드시 상속을 통해서 가능.
-public

*public

package ch06;

class Access1{
	public int a=0;
	
	public void prn() {
		System.out.println(a);
	}
	
	
}
public class AccessEx1 {
	public static void main(String[] args) {
		
	}
}

*private

package ch06;

class Access2{
	private int a=0;
	private int age=0;
	
	public int getA() {
		return a;
	}
	
	public void setA(int a) {
		this.a = a;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		if(age<0)
			age=0;
		this.age = age;
	}
	
}

public class AccessEx2 {
	public static void main(String[] args) {
		Access2 ac = new Access2();
		//ac.a=10;
	}
}

*protected

ch06 패키지

package ch06;

import ch06_1.Access3;

class Access4 extends Access3{
	public Access4() {
		//상속에서 public, protected 까지 유산
		//private은 제외
		int k=b+c;
	}
}
public class AccessEx4 {
	public static void main(String[] args) {
		Access3 ac=new Access3();
		
	}
}

ch06_1 패키지

package ch06_1;

//Access3.java
public class Access3{
	private int a;
	protected int b;
	public int c;
	
}

3. 번외

java.awt.Frame에서 paramString 의 결과값을 출력하는 법

package ch06;

import java.awt.Frame;

//java.awt.Frame에서 paramString 의 결과값을 출력
public class AccessEx5 extends Frame {
	public AccessEx5(){
		System.out.println(paramString());
	}
	public static void main(String[] args) {
		AccessEx5 ac=new AccessEx5();
	}

}

0개의 댓글