Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Calling constructor1() calls constructor2()... how?!

  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Calling constructor1() calls constructor2()... how?!

    I am writing a program where I am using two main objects which are identical in every way apart from the object name. Lets call them Object1 and Object2. Despite duplicating the code for declaration and initialisation, Java will work perfectly for Object1 but refuse to work for Object2.

    Here is the code at the start of the program:

    Java Code:
    public class MainClass{
    
    public static Object1[] object1array = new Object1[3];
    public static Object2[] object2array = new Object2[3];
    
    public static void main(String[] args){
    
    
    
    for(int i = 0; i < 3; i++){
    object1array[i] = new Object1(i);
    }
    
    
    
    for(int i = 0; i < 3; i++){
    object2array[i] = new Object2(i);
    }
    
    }
    Where Object1 and Object 2 are:

    Java Code:
    class Object1{
    
    int indexNumber;
    boolean available;
    
    public Object1(int iN){
    indexNumber = iN;
    available = true;
    }
    }
    
    
    class Object2{
    
    int indexNumber;
    boolean available;
    
    public Object2(int iN){
    indexNumber = iN;
    available = true;
    }
    }

    By running through the debugger, at the end of the attempted initialisation and filling of object2, the variables are as follows:

    object1array = initialised and array filled correctly with 3 object1 objects (beginning at index 0 and ending at index 2)
    object2array = null (No array, and obviously no elements)


    What is even stranger is that if i put the object2 initialisation loop before that of object1, object1 somehow gets initialised as if it was still first (Yes, I saved, and rebuilt fully).
    Last edited by JosAH; 03-13-2014 at 10:56 AM. Reason: restored original content

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Calling constructor1() calls constructor2()... how?!

    Looks like the posted code is missing some }s
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Calling constructor1() calls constructor2()... how?!

    Quote Originally Posted by Norm View Post
    Looks like the posted code is missing some }s
    Just the one at the end of the first block of code I think. I accidentally omitted the class declaration from the code block on the forum.

    Please ignore this thread for a second, i noticed I was using the debugger incorrectly so need to re-evaluate my code.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Calling constructor1() calls constructor2()... how?!

    Once you post some code or other info and get a reply, do not delete or edit the material which is the subject of the reply. Otherwise, it makes the thread meaningless. Hopefully, one of the mods will restore it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,086
    Rep Power
    4

    Default Re: Calling constructor1() calls constructor2()... how?!

    Quote Originally Posted by kkid View Post
    EDIT:

    I have no idea how, but it has solved itself. I did nothing to the files but I ran it for the ~700th time and it worked(ish)!
    I call BS. No code that "doesn't work" and is not touched will run if you try it 700 times.
    gimbal2 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Calling constructor1() calls constructor2()... how?!

    Quote Originally Posted by SurfMan View Post
    I call BS. No code that "doesn't work" and is not touched will run if you try it 700 times.
    Its actually pretty sad; if you're this adamant to not admit you were screwing it up, you'll never be a good programmer. Admitting failure as soon as possible is rather important to keeping your job, because the sooner you give in, the sooner you're on the path to fixing your own shit.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. SecurityFilter EJB calls
    By sirair in forum Java Servlet
    Replies: 0
    Last Post: 06-23-2013, 06:25 PM
  2. recursive calls count
    By emosms in forum New To Java
    Replies: 7
    Last Post: 06-07-2011, 11:54 PM
  3. static constuctor calls?!
    By camel in forum New To Java
    Replies: 2
    Last Post: 02-13-2011, 06:43 PM
  4. Making calls from pc
    By ilyaufo in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 05-17-2010, 05:38 PM
  5. Java System Calls
    By jonnytabpni in forum Advanced Java
    Replies: 3
    Last Post: 03-03-2010, 08:04 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
  •