#멋쟁이사자처럼 #부트캠프 #백엔드 #JAVA

System.out.println();

Static Field VS Dynamic Field

Static fields can be accessed by a dynamic method but you need to create an instance to access a dynamic fields from a static method.

class ExampleClass {
    int num = 32;
}

public class Practice04 {
    public static void main(String[] args) {
        // ❌ Cannot make a static reference to the non-static field ExampleClass.num
        System.out.println(ExampleClass.num);
    }
}
class ExampleClass {
    static int num = 32;
}

public class Practice04 {
    public static void main(String[] args) {
        System.out.println(ExampleClass.num);
    }
}

Practice: My Info

public class Practice01 {
    static String name = "Muffin";
    static String gender = "Nonbinary";
    static String email = "[email protected]";

    public static void main(String[] args) {
        System.out.println("Name: " + name);
        System.out.println("Gender: " + gender);
        System.out.println("Email: " + email);
    }
}

Practice: Rectangle

class Rectangle {
    int width;
    int height;

    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public int getArea() {
        return width * height;
    }
}

public class Practice02 {
    public static void main(String[] args) {
        Rectangle rect = new Rectangle(30, 5);
        System.out.println(rect.getArea()); // 150
    }
}

Practice: Asterisk Rectangle

public class Practice03 {
    public static void main(String[] args) {
        int width = 12;
        int height = 6;

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                System.out.print("*");
            }
            System.out.print("\\n");
        }
    }
}