天天看點

No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing

No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing instance of type SmsUtils (e.g. x.new A() where x is an instance of SmsUtils).

今天在寫一個短信發送的工具類時使用到了内部類,在執行個體化内部類時遇到此錯誤。

SmsResult result = new SmsResult();

沒有可以通路SmsUtils類型的封閉執行個體。必須使用封閉的SmsUtils類型的執行個體來限定配置設定(例如,x.new A(),其中x是SmsUtils的一個執行個體)。

SmsResult 是一個(非靜态的)内部類。 正在從靜态上下文建立它。您需要提供一個Server執行個體作為外部執行個體。但是,幾乎可以肯定我想讓 SmsResult 成為一個靜态的嵌套類,或者可能是一個外部類。

剛好今天看到一篇文章中提到: 

今天面試我問你static關鍵字有哪些作用,如果你答出static修飾變量、修飾方法我會認為你合格,答出靜态塊,我會認為你不錯,答出靜态内部類我會認為你很好,答出靜态導包我會對你很滿意,因為能看出你非常熱衷研究技術。

給 SmsResult 類的定義添加 static 關鍵字就ok了。