logo

예제가 포함된 Java의 정적 메소드

static 키워드는 클래스의 인스턴스 생성 여부에 관계없이 존재하는 메서드를 구성하는 데 사용됩니다. static 키워드를 사용하는 모든 메서드를 정적 메서드라고 합니다.

정적 메소드의 특징:



  • Java의 정적 메소드는 해당 클래스의 인스턴스가 아닌 클래스의 일부인 메소드입니다.
  • 클래스의 모든 인스턴스는 메서드에 액세스할 수 있습니다.
  • 정적 메소드는 클래스의 객체(인스턴스)를 사용하지 않고 클래스 변수(정적 변수)에 액세스할 수 있습니다.
  • 정적 메서드에서는 정적 데이터에만 액세스할 수 있습니다. 정적이 아닌 데이터(인스턴스 변수)에는 액세스할 수 없습니다.
  • 정적 메서드와 비정적 메서드 모두 정적 메서드에 직접 액세스할 수 있습니다.

정적 메서드를 선언하는 구문은 다음과 같습니다.

25c ~ k
Access_modifier static void methodName() { // Method body. }>

클래스 이름을 사용하여 정적 메서드를 호출하거나 액세스할 수 있습니다.

정적 메서드를 호출하는 구문은 다음과 같습니다.

className.methodName();>

예제 1: 정적 메서드는 인스턴스 변수에 액세스할 수 없습니다.

JVM은 먼저 정적 메서드를 실행한 다음 클래스 인스턴스를 생성합니다. 정적 메서드를 사용하면 개체에 액세스할 수 없기 때문입니다. 정적 메서드는 인스턴스 변수에 액세스할 수 없습니다. 결과적으로 정적 메서드는 클래스의 인스턴스 변수에 액세스할 수 없습니다.

자바




자바 프로그램 루프

// Java program to demonstrate that> // The static method does not have> // access to the instance variable> > import> java.io.*;> > public> class> GFG {> >// static variable> >static> int> a =>40>;> > >// instance variable> >int> b =>50>;> > >void> simpleDisplay()> >{> >System.out.println(a);> >System.out.println(b);> >}> > >// Declaration of a static method.> >static> void> staticDisplay()> >{> >System.out.println(a);> >}> > >// main method> >public> static> void> main(String[] args)> >{> >GFG obj =>new> GFG();> >obj.simpleDisplay();> > >// Calling static method.> >staticDisplay();> >}> }>

>

>

자바 문자열 메소드
산출

40 50 40>

예제 2: 정적 메서드와 비정적 메서드 모두 정적 메서드에 직접 액세스됩니다.

자바




// Java program to demonstrate that> // In both static and non-static methods,> // static methods are directly accessed.> > import> java.io.*;> > public> class> StaticExample {> > >static> int> num =>100>;> >static> String str =>'GeeksForGeeks'>;> > >// This is Static method> >static> void> display()> >{> >System.out.println(>'static number is '> + num);> >System.out.println(>'static string is '> + str);> >}> > >// non-static method> >void> nonstatic()> >{> >// our static method can accessed> >// in non static method> >display();> >}> > >// main method> >public> static> void> main(String args[])> >{> >StaticExample obj =>new> StaticExample();> > >// This is object to call non static function> >obj.nonstatic();> > >// static method can called> >// directly without an object> >display();> >}> }>

C++ 문자열 분할
>

>

산출

static number is 100 static string is GeeksForGeeks static number is 100 static string is GeeksForGeeks>

정적 메서드를 사용하는 이유는 무엇입니까?

  1. 정적 변수 및 기타 객체 기반이 아닌 정적 메서드에 액세스하고 변경합니다.
  2. 유틸리티 및 지원 클래스는 정적 메서드를 자주 사용합니다.

정적 메서드의 제한 사항:

  1. 비정적 데이터 멤버 또는 비정적 메서드는 정적 메서드에서 사용할 수 없으며 정적 메서드는 비정적 메서드를 직접 호출할 수 없습니다.
  2. 정적 환경에서는 this와 super를 사용할 수 없습니다.

Java의 주요 메소드입니다. 공전?

객체에 정적 메서드를 호출할 필요가 없기 때문입니다. 비정적 함수인 경우 JVM은 main() 메서드를 호출하기 전에 먼저 객체를 빌드하므로 추가 메모리 할당이 어려워집니다.

정적 메소드와 인스턴스 메소드의 차이점

인스턴스 메소드

정적 방법

클래스의 객체가 필요합니다. 클래스의 객체가 필요하지 않습니다.
클래스의 모든 속성에 액세스할 수 있습니다. 클래스의 정적 속성에만 액세스할 수 있습니다.
메서드는 개체 참조를 통해서만 액세스할 수 있습니다. 이 메서드는 클래스 이름으로만 액세스됩니다.
구문: Objref.methodname() 구문: className.methodname()
이는 값별 프로그래밍의 예입니다. 이는 참조별 프로그래밍의 예입니다.