Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating an array in constructor ... defaults all values?

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Creating an array in constructor ... defaults all values?

    For some reason I am having a very difficult time trying to initialize and store values in arrays within a constructor. When I use the debugger to check the arrays values, the Boolean all default to false and my other double array all default to 0s.

    Pretend I have this:

    public class shank
    {
          double h;
          double m;
          boolean t1;
          boolean t2;
          boolean t3;
          boolean t4;
          int peeps;
          double [] huy = new double [3];
          boolean [] rup = new boolean [3];
     
          shank(int popes, boolean r1; boolean r2; boolean r3; boolean r4) // two of these are true and 2 are false (boolean)
          {
                peeps = popes;
                t1 = r1;
                t2 = r2;
                t3 = r3;
                t4 = r4;
     
                double [] huy = {586.1, 429.3, 392.3, 3492.3}; //this defaults to all 0s.
                boolean [] rup = {t1, t2, t3, t4};  //this defaults to all false.
     
          }
    }

    I figured the arrays default because I declare them twice, but if I don't, then I just get an error...Can someone direct me the correct way of getting my arrays correctly initialized. I should mention that I have two classes, one is the tester and this is the class that is called with the methods. I prefer that no more parameters be added to the constructor.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating an array in constructor ... defaults all values?

    You need to define the variables at the class level and assign them values in the constructor.
    You are defining two different variables with the same names. When the constructor exits, the local variables: huy and rup go away.

    You can probably move the definitions from the constructor to the class level and replace the class level definitions with the moved ones.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating an array in constructor ... defaults all values?

    Ah, I did something like that, and got it working. Before I realized this forum was back to normal, I had the guys at Stack Overflow forum help me out. Thanks though.

Similar Threads

  1. Trouble using enum in constructor when creating a class
    By willmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 13th, 2011, 10:48 AM
  2. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 09:51 PM
  3. Constructor doesn't fill in array properly
    By Whyareall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2010, 04:24 AM
  4. [SOLVED] Array of objects, invoking constructor for one changes others
    By BigFoot13 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 24th, 2010, 01:30 PM
  5. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 11:02 AM