본문 바로가기

JavaCode(review)

Final statement / 1.개념 코딩/ 2. password 암호화 코딩

반응형

다른 개발자가 실수로라도 값 변경 never / 보통 상수 define시 사용됨 ex) Math.PI =3.xxxxx ( from final class math)

즉, 한번 assigned된 final 값은 변경 불가

 

예) final field, final method, final class

private final int a = 1;
public final void add {
}

public final class Math{

                                      }

 

// SomeClass  : final 개념설명 위한

package com.timbuchalka;

public class SomeClass {

    private static int classCounter = 0;            //static - memory
    public final int instanceNumber;        //final		V
    private final String name;              //final		V


    public SomeClass(String name) {     //constructor
        this.name = name;
        classCounter++;                 //constructor 실행시 증가(static- 저장된다.)
        instanceNumber = classCounter;
        System.out.println(name + " created, instance is " + instanceNumber); //final 값  V
    }

    public int getInstanceNumber() {  //getter    - final 값    V
        return instanceNumber;
    }

}

//Main Class  : 실행위한 코딩
package com.timbuchalka;

public class Main {

    public static void main(String[] args) {
	    SomeClass one = new SomeClass("one");       //object / call constructor
        SomeClass two = new SomeClass("two");		//또다른 ogject
        SomeClass three = new SomeClass("three");	//또다른 object

     //   one.instanceNumber = 4 ;		V	//instanceNumber는 final값으로 변경시도시, 오류.
     
        System.out.println(Math.PI);    // final class Math의 예
	}
}

//Output
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=49987:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\Naming-Conventions-and-Packages-The-final-statement-Source-code\out\production\Final com.timbuchalka.Main
one created, instance is 1		//assigned 1 ->이후 값 변경 불가(one object에서)		V
two created, instance is 2		//assigned 2 -> 이후 값 변경 불가(two object에서)   V
three created, instance is 3	//assinged 3 -> 이후 값 변경 불가(three object에서)  V
3.141592653589793				//Math.PI

Process finished with exit code 0

 

비밀번호 암호화 코딩 password

//Password Class		V

package com.timbuchalka;


public class Password {
    private static final int key = 748576362;          //암호화값 설정 위한? //final field
    private final int encryptedPassword;    //  암호화된 비밀번호  // final field

    public Password(int password) {                    // constructor
        this.encryptedPassword = encryptDecrypt(password);
    }                                          //constructor 실행시, 해당 method실행
                                                //password ^ key
    private int encryptDecrypt(int password) {
        return password ^ key;                  //XOR(^)의 표현 : 암호화값 설정????
    }

    public final void storePassword() {			//inheritacne한 타 class에서 override 불가
        System.out.println("Saving password as " + this.encryptedPassword); //암호화 값
    }

    public boolean letMeIn(int password) {      //암호화 값
        if(encryptDecrypt(password) == this.encryptedPassword) {
            System.out.println("Welcome");
            return true;
        } else {
            System.out.println("Nope, you cannot come in");
            return false;
        }
    }
}

// Main Class
package com.timbuchalka;

public class Main {

    public static void main(String[] args) {
	    
        int pw = 674312;
        Password password = new Password(pw);	//password값 설정
        password.storePassword();       //final값 //this.encryptedPassword; 암호화값

        System.out.println("");

        password.letMeIn(1);
        password.letMeIn(748576362);		//key 값
        password.letMeIn(674312);   		// password 값
        password.letMeIn(747902050); 		// 암호화값
        password.storePassword();			//암호화값
    }
}

//OutPut

Saving password as 747902050		//암호화값

Nope, you cannot come in			//1
Nope, you cannot come in			//key
Welcome								//password
Nope, you cannot come in			//암호화값
Saving password as 747902050		//암호화값

'JavaCode(review)' 카테고리의 다른 글

binary search tree  (0) 2020.07.01
Static initialization block  (0) 2020.06.30
Static Statement  (0) 2020.06.29
Account coding / access modifier 'private' example  (0) 2020.06.28
Scope 개념 코딩  (0) 2020.06.28