Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:33:23+00:00 2026-06-04T12:33:23+00:00

here , i am creating file server program .in this i have noticed that

  • 0

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 .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:33:24+00:00Added an answer on June 4, 2026 at 12:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is how I'm creating a file: System.IO.File.Create(Server.MapPath(..) + name + .html); But sometimes
I started creating file downloader class. Here is what i have: public class Downloader
I'm creating a script that finds all the error_log files on my server. Here's
I am creating a program that will download a .jar (java) file from a
Here's something that has been bugging me... I am currently creating an application for
I am creating a installer using WIX, The requirement here is that when the
I have a web application that creates directories. The application works fine when creating
I'm creating asp.net special file manager. all file information saved to SQL Server table.
I have a bit of Java code that outputs an XML file to a
I'm creating a basic concurrent server allowing a user to download a file using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.