프로그래밍

자바와 닷넷 비교 - static

Jinwookoh 2025. 3. 2. 20:17

C#과 Java에서 static 키워드는 거의 동일한 개념을 가지지만, 몇 가지 중요한 차이점이 있습니다.


1. static의 기본 개념

C#과 Java 모두 static 키워드를 사용하여 클래스 수준에서 접근할 수 있는 멤버(변수, 메서드, 클래스)를 정의합니다.
즉, static 멤버는 인스턴스 없이 클래스 자체에서 접근할 수 있습니다.


2. static 변수 (클래스 변수)

C#

class Example
{
    public static int count = 0;

    public Example()
    {
        count++;
    }
}
Console.WriteLine(Example.count); // 0
Example ex1 = new Example();
Example ex2 = new Example();
Console.WriteLine(Example.count); // 2

Java

class Example {
    public static int count = 0;

    public Example() {
        count++;
    }
}
System.out.println(Example.count); // 0
Example ex1 = new Example();
Example ex2 = new Example();
System.out.println(Example.count); // 2

✅ 차이점 없음

C#과 Java 모두 static 변수는 모든 인스턴스에서 공유됨.


3. static 메서드

C#

class Utility
{
    public static void PrintMessage()
    {
        Console.WriteLine("Hello from C#");
    }
}
Utility.PrintMessage(); // Hello from C#

Java

class Utility {
    public static void printMessage() {
        System.out.println("Hello from Java");
    }
}
Utility.printMessage(); // Hello from Java

✅ 차이점 없음

C#과 Java 모두 static 메서드는 인스턴스 없이 클래스 이름을 통해 호출됨.


4. static 클래스

C#

C#에서는 클래스를 static으로 선언 가능하며, static 클래스는 인스턴스화할 수 없음.

static class MathHelper
{
    public static int Square(int x)
    {
        return x * x;
    }
}
// MathHelper math = new MathHelper(); // ❌ 컴파일 오류
Console.WriteLine(MathHelper.Square(5)); // 25

Java

Java에는 static class 개념이 없음.

  • Java에서 static 클래스가 필요한 경우 정적 내부 클래스로 구현.
class Outer {
    static class MathHelper {
        static int square(int x) {
            return x * x;
        }
    }
}
System.out.println(Outer.MathHelper.square(5)); // 25

🔥 차이점

  • C#에서는 **완전히 정적인 클래스(static class)**를 선언 가능
  • Java에서는 **정적 내부 클래스(static nested class)**로만 사용 가능

5. static 생성자

C#

  • 클래스가 처음 로드될 때 한 번만 실행
  • 매개변수 없음
class Example
{
    public static int count;
    
    static Example()
    {
        count = 10;  // 클래스 로드 시 1번 실행
    }
}

Java

  • Java에는 static 생성자가 없음
  • 대신, 정적 블록(static block) 사용
class Example {
    public static int count;
    
    static {
        count = 10;  // 클래스 로드 시 1번 실행
    }
}

🔥 차이점

  • C#: static 생성자 (static Example() { }) 사용
  • Java: static 블록 (static { }) 사용

6. static과 다형성 (오버라이딩)

C#

C#에서 static 메서드는 오버라이딩 불가, 하지만 숨김(Hiding) 가능

class Parent
{
    public static void Show()
    {
        Console.WriteLine("Parent Show");
    }
}

class Child : Parent
{
    public new static void Show()
    {
        Console.WriteLine("Child Show");
    }
}
Parent.Show(); // Parent Show
Child.Show(); // Child Show

Java

Java에서도 static 메서드는 오버라이딩 불가, 하지만 숨김(Hiding) 가능

class Parent {
    static void show() {
        System.out.println("Parent Show");
    }
}

class Child extends Parent {
    static void show() {
        System.out.println("Child Show");
    }
}
Parent.show(); // Parent Show
Child.show(); // Child Show

✅ 차이점 없음

  • C#과 Java 모두 static 메서드는 오버라이딩 불가 (숨길 수는 있음)

7. static import

C#

C#에서는 static import 개념이 없음.

  • 대신, using static을 통해 클래스의 정적 멤버를 가져올 수 있음.
using static System.Console;
WriteLine("Hello"); // Console.WriteLine() 대신 사용 가능

Java

Java에서는 import static 사용 가능.

import static java.lang.System.out;
out.println("Hello"); // System.out.println() 대신 사용 가능

🔥 차이점

  • C#: using static
  • Java: import static

8. static 필드의 초기화 순서

C#과 Java 모두 정적 필드는 선언 순서대로 초기화됨.

class Example
{
    public static int A = B + 1;  // B 초기화 후 실행
    public static int B = 10;
}

Console.WriteLine(Example.A); // 11
class Example {
    static int A = B + 1;  // B 초기화 후 실행
    static int B = 10;
}

System.out.println(Example.A); // 11

✅ 차이점 없음

  • C#과 Java 모두 정적 필드는 위에서 아래로 초기화

✅ C#과 Java static 차이 정리

특징 C# JAVA
static 변수 O O
static 메서드 O O
static 클래스 O ❌ (대신 static nested class 사용)
static 생성자 O (static Example() { }) ❌ (대신 static { } 블록 사용)
static 메서드 오버라이딩 ❌ (숨김 가능) ❌ (숨김 가능)
static import using static import static

🔥 가장 큰 차이점

  1. C#은 static class 선언 가능, Java는 불가능 (대신 static 내부 클래스 사용)
  2. C#은 static 생성자 지원, Java는 static 블록 사용
  3. C#은 using static, Java는 import static 사용