제가 짜고 있는 코드인데, 제가 기본이 없고 자바를 시작한지 얼마 안되서
main함수를 왜 이렇게 선언하는지에 대한 이유를 잘모릅니다..
public static으로 선언하는 이유가 클래스를 생성하지 않아도 아무 클래스에서든지 불러와서 사용할 수 있게 하기 위함. 으로
알고 있는데,매개변수가 String클래스로 되어있어서 커멘드라인으로 String을 입력받을수도 있구요.
라고 책에 나와있는것을 알고 있을뿐, 원리를 알진 못해서 결국 오류를 해결하지 못하고 있습니다.
class Data
{
double num=0;
void Setnum()
{
num = Math.random()*1000;
}
double Getnum()
{
return num;
}
}
class thread extends Thread
{
Data thread_data;
public thread(String str, Data D)
{
super(str);
thread_data=D;
}
public void run()
{
try {
for(int i=0; i<10; i++){
sleep(1000);
thread_data.Setnum();
System.out.println(getName()+"쓰레드의 임의의 숫자 : "+thread_data.Getnum());
}
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
public class jun
{
public static void main(String[] args)
{
Data ddd = new Data();
Thread t1 = new thread("첫번째", ddd);
Thread t2 = new thread("두번째", ddd);
Thread t3 = new thread("세번째", ddd);
t1.start();
t2.start();
t3.start();
}
}
쓰레드를 3개 생성해서 임의의 숫자를 리턴받는 코드인데요,
Exception in thread "main" java.lang.Error: 분석되지 않는 컴파일 문제점:
main 메소드는 static으로 정의될 수 없습니다. static 메소드는 static 또는 맨 위 레벨 유형에서만 정의될 수 있습니다.
at thread$jun.main(jun.java:35)
이렇게 오류가 나네요.
30분째 헤매고 있습니다..힌트좀 주세요ㅜㅜ
{ } 문제
} 빠졋네
ส++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
클래스가 몇개나 있는거야