prjndhi
prjndhi

Reputation: 1965

why in file server program socket write as "Socket s =null"?

here , i am creating file server program .in this i have noticed that socket s= null is written.i want to know actual reason why null is given.I thought that it is either related to the ObjectInputStream or Scanner.is it true it related to the ObjectInputStream or Scanner .Here the code for

Server.java 

    public class Server{
public static void main(String[] args){
Socket s=null;
ServerSocket ss=null;
ObjectInputStream ois=null;
ObjectOutputStream oos=null;
Scanner sc=new Scanner(System.in);


try
    {
ss = new ServerSocket(1234);
System.out.println("server is created");

    }
catch(Exception e)
    {
System.out.println(e.getMessage());
    }



try {
s=ss.accept();
System.out.println("connected");
oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject("Welcome");
ois= new ObjectInputStream(s.getInputStream());
    }catch(Exception e)
    {
e.printStackTrace();
    }
try{
String fil=(String)ois.readObject();
FileInputStream fis = new FileInputStream(fil);
int d;
String data="";
while(true)
        {
d=fis.read();
if(d==-1)
    break;
data = data+(char)d;
        }
oos.writeObject(data);
    }
catch(Exception e)
    {
System.out.println(e.getMessage());
    }
}
}

can anyone explain actual reason? Thanks in advance .

Upvotes: 0

Views: 480

Answers (3)

pap
pap

Reputation: 27604

The answer to your question is that it's considered good practice to initialize variables to null if they are declared and assigned separately (rather than letting the compiler/execution environment initialize them for you. This goes back to early C where it was not guaranteed what value local variable was initialized to unless explicitly set.

The Java compiler will generate an error if it detects that a variable is accessed without having been initialized or set. For example:

    String s;
    try {
        s = "abc";
    } catch (Exception e) {

    }
    System.out.println(s);

Will generate a compiler error on the last line, stating that the local variable may not have been initialized (since if the try-clause throws an exception before s is assigned, it will never be set).

In your case, this does not seem to be an issue since even if you are declaring your Socket outside of your try-block, you are never accessing it outside of the try-block scope.

Upvotes: 1

Kris
Kris

Reputation: 8873

As far as I know, its actually not because of a reason. But, In general its a good coding practise to make sure that, all your variables are initialized before they are used.

So, since you dont want to assign a new socket, untill an accept() is called, you will just assign it as null.

This wont make sense in this context, but still if you are passing the uninitialized variable to some where, it will generate a warning.

Upvotes: 0

rlinden
rlinden

Reputation: 2041

I do not know if I understood you correctly, but if you are talking about the first lines of your code, then it is a simple initialization.

Good programming practices state that you should not let the programming language implementation initialize your variables. Even if the standard states that the initial value will be a specific one, standards can change and implementations may be faulty at some point. Hence, you should always initialize your variables with their expected initial values.

In the case of your code, null value means that a connection is non existant. Therefore, when initializing your variables, given that you have not yet connected, you store null in all your objects that represent sockets.

I hope this helps.

Upvotes: 1

Related Questions