3 Replies Latest reply on Feb 12, 2008 11:44 PM by 807601

    Random Access File creator errors

    807601
      Hello. I am working on a project, and I am having some trouble with my code.

      My main question is is it possible to create and use a Random Access File as part of a class, but outside of any of the classes methods? I have written my code that way, but when I compile, I get an error about the syntax. Below is the code and error message:

      Error Message (using gcj on Ubuntu Linux to compile):

      Reader.java:7: error: Syntax error on token "throws", @ expected
      static RandomAccessFile bank = new RandomAccessFile("bank.txt", "rw"); throws IOException
      ^^^^
       public class Reader
      {
           static RandomAccessFile bank = new RandomAccessFile("bank.txt", "rw"); throws IOException
      If anyone can give me some suggestions, I'd greatly appreciate it.

      DarkFerret
        • 1. Re: Random Access File creator errors
          807601
          Declare the variable but don't initialize it. Then construct the RAF inside of a try/catch in a static block.

          Honestly though, this seems a little weird.
          • 2. Re: Random Access File creator errors
            807601
            Thank you. That solved the problem. Took me a min to figure out the static block, but I did. Thanks for your help.

            Edited by: DarkFerret on Feb 12, 2008 3:32 PM
            • 3. Re: Random Access File creator errors
              807601
              DarkFerret wrote:
              Hello. I am working on a project, and I am having some trouble with my code.

              My main question is is it possible to create and use a Random Access File as part of a class, but outside of any of the classes methods? I have written my code that way, but when I compile, I get an error about the syntax. Below is the code and error message:
              This is a big mistake. Make that bank an instance variable. You can give a variable class scope WITHOUT it being static. You should do that.

              >
              Error Message (using gcj on Ubuntu Linux to compile):
              This is another mistake. Get rid of gcj it is terrible garbage. Replace it with the Sun JDK, there is one for ubuntu so you won't have much trouble doing this.

              If you stick with gcj I assure you you will run into to all sorts of unpleasant runtime errors that you will find difficult to diagnose and fix because gcj is non-compliant, bug ridden trash.