반응형
static
- 클래스 내의 함수나 변수는 클래스가 객체를 생성할 때까지 메모리에 인스턴스가 생성되지 않는다.
- 그러나 static을 사용해 함수나 변수를 선언하면, 메모리에 인스턴스가 직접 생성되고, 전역적으로 작동한다.
- 또한 어떠한 객체도 참조하지 않는다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Static_var_and_fun
{
class number
{
public static int num; // 정적 변수 선언
public static void power() // 정적 함수 선언
{
Console.WriteLine("Power of {0} = {1}", num, num * num);
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number\t");
// 아래와 같이 클래스명으로 변수, 함수에 접근할 수 있음
number.num = Convert.ToInt32(Console.ReadLine());
number.power();
}
}
}