Results 1 to 6 of 6
  1. #1
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default Double constructors, with and without array

    here is the code i got:
    Java Code:
    class test {
    	private final String abc;
    	private final int x;
    	private final int[] y;
    	private final int[] z;
    
    	public test(String abc, int x){
    		this.abc = abc;
    		this.x = x;
    	}
    
    
    	public test(String abc, int x, int[] z, int[] y) {
    		this.abc = abc;
    		this.x = x;
    		this.z = z;
    		this.y = y;
    	}
    }
    and the error message
    Java Code:
    test.java:41: error: variable y might not have been initialized
    	}
    	^
    1 error
    
    Tool completed with exit code 1
    what is wrong with the code and how should it look like ?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Double constructors, with and without array

    The error message tells you exactly what is wrong: variable y might not be initialised. The error occurs on line 41 which you have not posted.

  3. #3
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default Re: Double constructors, with and without array

    Quote Originally Posted by Junky View Post
    The error message tells you exactly what is wrong: variable y might not be initialised. The error occurs on line 41 which you have not posted.
    the rest is commented away so that is the whole code. removed all of the comments and still;

    Java Code:
    class test {
    	private final String abc;
    	private final int x;
    	private final int[] y;
    	private final int[] z;
    
    	public test(String abc, int x){
    		this.abc = abc;
    		this.x = x;
    	}
    
    
    	public test(String abc, int x, int[] z, int[] y) {
    		this.abc = abc;
    		this.x = x;
    		this.z = z;
    		this.y = y;
    	}
    }
    Java Code:
    test.java:10: error: variable y might not have been initialized
    	}
    	^
    1 error
    
    Tool completed with exit code 1
    if you look at line 10, y is not there, it's one "}" bracket. and the variables at use there is abc, x. not y.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Double constructors, with and without array

    You have declared your variables as final. This means that you can never change the value of those variables. At the end of the first constructor what value will y (and z) have?

  5. #5
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default Re: Double constructors, with and without array

    shait, removed final and all worked perfectly. got tons of things to learn still.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Double constructors, with and without array

    Quote Originally Posted by Juukamen View Post
    removed final
    Is that the correct solution? Maybe y and z should be final? If they are then you need to assign them default values in the first constructor.

Similar Threads

  1. Replies: 2
    Last Post: 08-12-2011, 08:48 PM
  2. Double Array problem
    By AlinkaTroll in forum New To Java
    Replies: 6
    Last Post: 05-03-2011, 01:58 AM
  3. returning double array
    By Billaguana in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 03:59 AM
  4. Array of 4 bytes to double.
    By blind melon in forum Advanced Java
    Replies: 4
    Last Post: 12-08-2010, 11:19 AM
  5. Array constructors
    By StanO in forum New To Java
    Replies: 4
    Last Post: 11-05-2010, 09: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
  •