Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

variable might not been initialized

44577Oct 9 2010 — edited Oct 9 2010
I encounter this error whilst compiling the source. The code is as below:
public final class A{
    private final MulticastSocket socket;
    public A(int port){
        try{
            this.socket = new MulticastSocket(port);
        }catch(Exception e){}        

    }
}
The error says `Variable socket might not have been initialized.'

After reading http://java.sun.com/docs/books/jls/second_edition/html/defAssign.doc.html, I think this might be from the construction with try ... catch statement because it is not guaranteed to be executed `as the rules of definite assignment are concerned'.

How can I avoid this error? (I notice I can replace try catch block with `public A (int port) throws Exception' to avoid this compilation error, but this is not what I want.)

I appreciate any suggestion.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 6 2010
Added on Oct 9 2010
6 comments
535 views