views:

90

answers:

5

I have a code with the following sequence of lines:

Socket echoSocket = null;
... something ...
echoSocket = new Socket("taranis", 7);

I do not understand why we want to have a first line. Well I know that Java is unable to define a type of the variable from its value. It's why first we need to tell that echoSocket is variable which has Socket type (the first line) and than we tall that echoSocket has a certain value (an object of a class Socket).

But what I do not understand is why we need to assign a value two times? Why do we want to say that echoSocket is equal to null?

+1  A: 

Why are you declaring the variable earlier than you have a useful value for it anyway? Occasionally this is necessary if it's going to be assigned in two different branches, but generally it's better to wait until you have a value, and declare and assign in one statement:

Socket echoSocket = new Socket("taranis", 7);
Jon Skeet
I have an idea (not sure if it is good). Maybe the assignment is not always successful? We try to use "new Socket" in a "try" block(to set a value to the echoSocket) and than we assume that echoSocket has a value. But there is no guaranty that "new Socket" worked. So it is better to assign a value to the echoSocket before the assignment in the try block. Just to have a value for sure.
Roman
@Roman: If the socket constructor failed, are you going to be able to continue in your method anyway? That *sort* of situation is one of the reasons to assign null instead, but usually I find I want the exception to be immediately rethrown anyway.
Jon Skeet
+1  A: 

You don't need to give echoSocket the value of null. (Where did you read that?)

You can go with either

Socket echoSocket;
... something ...
echoSocket = new Socket("taranis", 7);

or do it in one line

... something ...
Socket echoSocket = new Socket("taranis", 7);

Hope it helps..

Shaharyar
I got the code from here: http://java.sun.com/docs/books/tutorial/networking/sockets/readingWriting.html
Roman
+2  A: 

You do not need to assign a value to a local variable is it is not used. Code should generatlly not declare variables until necessary. There is a somewhat dull chapter in the JLS on definite assignment.

You problem seems to be with try-blocks. Resource handling should be written as:

acquire();
try {
    use();
} finally {
    release();
}

In this case:

final Socket echoSocket = new Socket("taranis", 7);
try {
    ... something ...
} finally {
    echoSocket.close();
}

Catching of exception should go around the lot. Don't try to save on try blocks.

If you find yourself repeating a lot of boilerplate, try the Execute Around Idiom.

Tom Hawtin - tackline
+4  A: 

The general principle I use is this: declare a variable as late as possible.

There is one very useful case for not initializing a variable however:

String someString;
if (/* some condition */) {
  someString = "foo";
} else {
  someString = "bar";
}

Because someString is unitialized on declaration if, say, the else clause didn't set a value the Java compiler would complain about unitialized values. That wouldn't be the case if you did this:

String someString = null;
if (/* some condition */) {
  someString = "foo";
} else {
  // do nothing
}

It's a good sanity check. The above isn't a compile error but this is:

String someString;
if (/* some condition */) {
  someString = "foo";
} else {
  // do nothing
}
cletus
A: 

Technical writers don't always write the best code.

Personally I only write initial values when the compiler tells me to.

EJP