Results 1 to 7 of 7
  1. #1
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default Assign a value to a final variable

    http://en.wikipedia.org/wiki/Final_(Java)

    According to the above link, I can assign a value to a final variable in the constructor once. The value in my program comes from args[] via the Command-line arguments. The class only has one constructor. The comiple gave me an error saying

    "variable processTime might not have been initialized"

    The processTime is the final variable (public final int processTime;)

    Is there a way to fix it?

    Thank you very much.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Test
    {
    	public String fName;
    	public final int processTime;
        public Test(String f)
        {
    		int l = 0;
    		try
    					   {
    
    					   File myFile= new File(f);
    					   FileInputStream fileStream = new FileInputStream(myFile);
    					   DataInputStream in = new DataInputStream(fileStream);
    					   BufferedReader br = new BufferedReader(new InputStreamReader(in));
    					   String strLine;
    					   strLine = br.readLine();
    					   System.out.println(strLine);
    					   System.out.println();
    					   strLine = br.readLine();
    					   Scanner scanner = new Scanner(strLine);
    					   l = scanner.nextInt();
    					   processTime = l;
    
    					}
    					catch (Exception e)
    				    {
    					   System.err.println("Error: " + e.getMessage());
    	    }
        }
      public static void main(String[] args)
      {
    	  String sFile= args[0];
    	  Test test = new Test(sFile);
      }
    }
    The value is 5 and it is in a text file. My goal is to assign the prosseingTime to be 5. If you need to try it, just put 5 in a file and use the file name as command-line argument.

    Thank you for help

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is that you're assigning the final variable from within a try block, and there's no guarantee that the assignment code line will ever be reached. If you want to assign it a value only if the end of the try is reached, and if not, assign it another default value, consider doing this in a finally block -- a block that will always be called. e.g.:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Test {
       private static final int PROCESS_TIME_DEFAULT_VALUE = -1;
       public String fName;
       public final int processTime;
    
       public Test(String f) {
          int l = 0;
          boolean tryCompleted = false;
          try {
    
             File myFile = new File(f);
             FileInputStream fileStream = new FileInputStream(myFile);
             DataInputStream in = new DataInputStream(fileStream);
             BufferedReader br = new BufferedReader(new InputStreamReader(in));
             String strLine;
             strLine = br.readLine();
             System.out.println(strLine);
             System.out.println();
             strLine = br.readLine();
             Scanner scanner = new Scanner(strLine);
             l = scanner.nextInt();
             //!! processTime = l;
             tryCompleted = true;  //!! or where you want to place this
          } catch (Exception e) {
             System.err.println("Error: " + e.getMessage());
          } finally {
             if (tryCompleted) {
                processTime = l;
             } else {
                processTime = PROCESS_TIME_DEFAULT_VALUE;  // ? default value
             }
          }
       }
    
       public static void main(String[] args) {
          String sFile = args[0];
          Test test = new Test(sFile);
       }
    }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

  6. #6
    yma16 is offline Member
    Join Date
    Apr 2011
    Posts
    52
    Rep Power
    0

    Default

    What is wrong with this code? It says "cannot assign a value to final variable x".

    Java Code:
    public class Test3
    {
    	public static final int x;
    	public static void main(String[] args)
    	 {
    		x=2;
    	 }
    }
    Thanks

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Final variable-constructor method.
    By D4rkNrG in forum New To Java
    Replies: 9
    Last Post: 10-13-2010, 11:18 AM
  2. about Final variable
    By Ramanjaneyulu in forum Advanced Java
    Replies: 6
    Last Post: 08-17-2010, 09:49 AM
  3. Assign it, or call it again,, (again!)
    By sonny in forum New To Java
    Replies: 4
    Last Post: 03-17-2010, 02:47 AM
  4. Replies: 2
    Last Post: 12-05-2009, 11:24 AM
  5. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 03:01 PM

Posting Permissions

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