Results 1 to 5 of 5
  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default constructor for FileOutputStream

    all,

    I have this code here:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    class tester
    {
        public static void main(String[] args)
        {
            FileOutputStream fstream;
            PrintWriter file;
            
            fstream = new FileOutputStream("c:\test.txt");
            file = new PrintWriter(fstream);
        }
    }
    I know this is not perfect and that it will throw and error, but my question is this:

    => why are the first two lines in the main method necessary? I can't remember what that type of coding is called again. Are those lines the initializations? What are those 2 lines and what are the following two lines? What types of terminology can I put on these 2 blocks of code? Initializations and assignments? Constructions and assignments?

    I'm looking for terms that I can memorize here about what is happening so I can refer to it as rules. Again I'm mentioning how important *rules* are in Java! No differen't than C or C++ really. thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: constructor for FileOutputStream

    Those are 'import statements' and are basically compiler directives, i.e. they tell the compiler where to look for classes mentioned in your Java source code; that's why they have to occur near the top of your source code (only a 'package statement' may occur before the imports).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: constructor for FileOutputStream

    And the first two lines in the main() method are declarations - they say what type of thing the fstream and file variables represent. It is the last two lines that initialise the variables.

    Java (and C++ etc) are block scoped languages. That means that variables "exist" until the end of the {}-block where they are declared. In such languages it is a good style to put the declaration of the variables as close as possible to where the variables are first used. In your case you ought to put the declarations on the very same line as the initialisation:

    Java Code:
    import java.io.*;
     
    class Tester
    {
        public static void main(String[] args)
        {
            FileOutputStream fstream = new FileOutputStream("c:\test.txt");
            PrintWriter file = new PrintWriter(fstream);
        }
    }
    Contrast this with other types of language - JavaScript springs to mind - where it really is a good idea to declare things at the start of functions.

    Notice how Tester is given an uppercase T. It's just a habit, but it makes it clear to a casual reader what sort of thing Tester is. Likewise the unused import (java.util.*) is going to make someone reading the code stop and wonder why that's there, and have to think about it before they decide that there is no reason.

  4. #4
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: constructor for FileOutputStream

    Quote Originally Posted by pbrockway2 View Post
    And the first two lines in the main() method are declarations - they say what type of thing the fstream and file variables represent. It is the last two lines that initialise the variables.
    I sure would like to get some rules for this. Say for instance that, only objects have to be declared before a new instance of them are assigned to vars?

    That would be awesome. What kind of convention could I put to this? Got any ideas? Again for instance, type decs like int, bool and dbl can be assigned a value during declaration. Apparently an object like a file stream cannot?

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

    Default Re: constructor for FileOutputStream

    Don't confuse the declaration of a variable with the use of the new operator to construct instances which are (maybe) assigned to a variable. Declarations occur exactly once per variable. The new operator (and the assignment) can occur as many times as you want.

    The declaration and the initial assignment can occur on the same line for any type: primitive types like int, double etc and user defined types like classes.

    Other than "you've got to declare a variable before you use it" there are no rules. But I'm suggesting that "declare a variable as close as you can to its use: on the same line as its initialisation, if possible" is a good idea.

Similar Threads

  1. FileOutputStream constructor issue
    By zlloyd1 in forum New To Java
    Replies: 3
    Last Post: 02-01-2013, 05:46 AM
  2. FileOutputStream to GIF
    By dewitrydan in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 07:12 AM
  3. closing FileOutputStream?
    By mgrootsch in forum New To Java
    Replies: 1
    Last Post: 05-17-2010, 05:36 PM
  4. FileOutputStream gets NotSerializableException
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 09:38 PM
  5. FileOutputStream question...
    By SCS17 in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 05:30 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
  •