2010-03-16 8 views
6

음, 어쩌면 어리석은 질문 일 수도 있지만이 문제를 해결할 수는 없습니다. 내 ServiceBrowser 클래스에서자바가 내 생성자를 찾을 수없는 이유는 무엇입니까?

나는이 라인이 있습니다

ServiceResolver serviceResolver = new ServiceResolver(ifIndex, serviceName, regType, domain); 

을 그리고 컴파일러는 그것에 대해 불평. 그것은 말한다 :

public void ServiceResolver(int ifIndex, String serviceName, String regType, String domain) { 
     this.ifIndex = ifIndex; 
     this.serviceName = serviceName; 
     this.regType = regType; 
     this.domain = domain; 
    } 

ADDED : 내가 생성자에서 void을 제거 하고 작동 나는 ServiceResolver의 생성자가 않기 때문에

cannot find symbol 
symbol : constructor ServiceResolver(int,java.lang.String,java.lang.String,java.lang.String) 

이, 이상하다! 왜? 서명

public ServiceResolver(int ifIndex, String serviceName, String regType, String domain) { 
     this.ifIndex = ifIndex; 
     this.serviceName = serviceName; 
     this.regType = regType; 
     this.domain = domain; 
    } 
+2

'void '는 생성자가 아닌 메서드에 사용됩니다. – BalusC

+0

@Roman 다른 계정으로 나만의 질문에 답변을 드렸습니까? – Bozho

+0

@ 보 소, 아니요. 다른 로마는 다른 사람입니다. – Roman

답변

9

삭제 무효 님의 방법이 아닌 생성자 정의했습니다.

는 아무 것도 반환되지 않으며 간단한 방법입니다 ... 생성자의없는 void

5

에서

+0

보노 (Bonho), 다른 로마인은 다른 사람입니다. 나는 다른 계정에서 내 질문에 대답하지 않습니다. – Roman

2

를 제거합니다. 절대 아무것도!

이 같아야합니다

public ServiceResolver(int ifIndex, String serviceName, String regType, String domain) { 
     this.ifIndex = ifIndex; 
     this.serviceName = serviceName; 
     this.regType = regType; 
     this.domain = domain; 
    } 
0

자바 생성자는 자신의 서명에 반환 형식이없는 - 그들은 암시 적으로 클래스의 인스턴스를 돌려줍니다.

0

오신 것을 환영합니다. 모두 한 번 씁니다. Roman이 지적했듯이 생성자의 infront에서 "void"를 삭제해야합니다.

생성자는 반환 유형을 선언하지 않습니다. x = new X()와 같은 일을하기 때문에 이상하게 보일 수 있습니다. 하지만이처럼 고려할 수 :

// what you write... 
public class X 
{ 
    public X(int a) 
    { 
    } 
} 

x = new X(7); 

// what the compiler does - well sort of... good enough for our purposes. 
public class X 
{ 
    // special name that the compiler creates for the constructor 
    public void <init>(int a) 
    { 
    } 
} 

// this next line just allocates the memory 
x = new X(); 

// this line is the constructor 
x.<init>(7); 

도구의 좋은 세트는 실수 (그리고 많은 다른) 이런 종류의를 찾을 수 실행할 수 있습니다

그런 식으로 다른 일반적인 실수를 저지르고 우리 모두가 :-), 당신은 너무 많은 해결책을 찾고 바퀴를 회전하지 않습니다.

관련 문제