Results 1 to 7 of 7
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Why my code does not work without null ?

    Java Code:
    package myPackage;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintWriter;
    
    public class TestClass {
    	
    	public static void main(String[] args) {
    		
    		PrintWriter outputStream = null;
    		try
    		{
    			outputStream = new PrintWriter(new FileOutputStream("c:/stuff.txt"));
    		}
    		catch(FileNotFoundException e)
    		{
    		}
    		System.out.println("Writing to file.");
    		outputStream.println("TEST");
    		outputStream.close();
    	}
    }
    Hi,

    My question is:

    If I put

    PrintWriter outputStream;

    instead of

    PrintWriter outputStream = null;

    eclipse tells me to initilaze variable for lines:
    outputStream.println("TEST");
    outputStream.close();

    If it is happy with = null, why is it telling me to initilaze it ?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why my code does not work without null ?

    The Java compiler is quite fussy about insisting that you initialise a variable before you use it. Instance variables get initialised without you having to do anything, but variables declared within a method or constructor do have to be initialised before they are used. null is a perfectly good value with which to initialise variables.

  3. #3
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Why my code does not work without null ?

    It is a rule defined in the java language spec:
    Types, Values, and Variables
    Based upon the principle of Definite Assignment
    Definite assignment analysis - Wikipedia, the free encyclopedia

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why my code does not work without null ?

    But doesn't it get initilazed when I say
    outputStream = new PrintWriter(new FileOutputStream("c:/stuff.txt"));

    ?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why my code does not work without null ?

    Yes it would get initialised if that statement completed normally. But it might throw an exception and not get initialised. Then your code would go to the catch block, do nothing there and proceed on to the "outputStream.println("TEST");". That is your code would be using outputStream that had failed to be initialised.

    Contrast with

    Java Code:
    package myPackage;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintWriter;
     
    public class TestClass {
         
        public static void main(String[] args) throws FileNotFoundException {
             
            PrintWriter outputStream; // <-- not initialised
    
            outputStream = new PrintWriter(new FileOutputStream("c:/stuff.txt"));
            System.out.println("Writing to file.");
            outputStream.println("TEST");  // <-- the compiler knows you won't get here unless the 
                                           //     initialisation completed normally
            outputStream.close();
        }
    }

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why my code does not work without null ?

    Thanks for the clear answer,it is very clear.

    The compiler is indeed very clever. :)

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why my code does not work without null ?

    You're welcome.

Similar Threads

  1. Will this code work
    By rajat16 in forum XML
    Replies: 3
    Last Post: 09-24-2010, 11:52 AM
  2. I can't get my code to work?
    By ComicStix in forum New To Java
    Replies: 5
    Last Post: 09-19-2010, 03:15 PM
  3. Code does not work
    By scotts in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 09:37 AM
  4. Why this code don't work?
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 04-16-2010, 02:57 AM
  5. How does the '*' work in the code?
    By DrMath in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 09:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •