스파르타 spring 2기 TIL day10

fart man·2025년 12월 4일
import java.util.Random;
// =================================================================================
// STUDY: This is a simple program describing the character of the keyword, final
// =================================================================================

class Main {
    static class Circle {
        // You might think for final member variables, it doesn't matter
        // if it's static or not, but in reality

        // final static : means It's values will be constant for all the
        //                classes. And classes will all share this one PI variable.
        public final static double PI = 3.141592; 

        // final : means It's values will be different for each class instances,
        //         each instance having it's own radius that can only be set during construction
        private final double radius;

        private Circle() {
            radius = 0;
        }

        public Circle(double radius) {
            this.radius = radius;
        }

        public double getRadius() {
            return this.radius;
        }

        public double getArea() {
            return this.radius * this.radius * Circle.PI;
        }
    }

    public static void main(String[] args) {
        Circle c1 = new Circle(1);
        Circle c2 = new Circle(2);

        System.out.printf("c1 radius : %f\n", c1.getRadius());
        System.out.printf("c2 radius : %f\n", c2.getRadius());

        System.out.printf("c1 area : %f\n", c1.getArea());
        System.out.printf("c2 area : %f\n", c2.getArea());

        // ===================================================================================
        // But can we assign final variables later out side of member variable declarations?
        // ===================================================================================

        // this causes compile error
        // final int tmp;
        // System.out.printf("tmp : %d\n", tmp);

        // but this doesn't!
        final int tmp;
        System.out.printf("doing something before assigning tmp\n");
        tmp = 20;
        System.out.printf("tmp : %d\n", tmp);

        // and this also works! Meaning java final is very different from Go constants in a sense that it doesn't have to be determined at compile time
        final int tmp2;

        if (new Random().nextInt(100) < 50) {
            tmp2 = 69;
        } else {
            tmp2 = 420;
        }
        System.out.printf("tmp2 : %d\n", tmp2);

        // =========================================================================
        // and finally, final can prevent function arguments from being reassigned
        // =========================================================================
        print("hi"); // HAH! CHANGED YOUR SHIT!!
        printFinal("hi"); // hi
    }

    public static void print(String thing) {
        thing = "HAH! CHANGED YOUR SHIT!!";
        System.out.println(thing);
    }

    public static void printFinal(final String thing) {
        // thing = "HAH! CHANGED YOUR SHIT!!"; // causes compile error
        System.out.println(thing);
    }
}

0개의 댓글