Results 1 to 7 of 7
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default help initialization

    Java Code:
    
    /*State of this fish */
    	private int row, col, size, direction;
    
    	/** Simply initializes the state of the fish with these parameters */
    	public Fish(int row, int col, int size, int direction) {
    	
    		Fish fish = new Fish (row, col, size, direction);
    	}
    the error I am getting is: apparent infinite recursive loop. I am not sure how to initialize this correctly. Thank you for help.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: help initialization

    In your constructor (Fish) you're calling the same constructor again unconditionally; that's why the infinite recursion eats all your stack space; your constructor should simply set its member variables:

    Java Code:
    public Fish(int row, int col, int size, int direction) {
       this.row= row;
       this.col= col;
       this.size= size;
       this.direction= direction;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: help initialization

    thanks you !

  4. #4
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: help initialization

    hi i have another quick question.
    Im lost on how to implement this.
    i'm not sure how to set other's size to zero.
    Do I have to create a new type Fish that holds
    the same parameters except the size = 0?

    Java Code:
    /* This fish eats the other fish.  I.e. This fish's size is increased by
    	 * the size of the fish "other".  The size of "other" is set to 0. */
    
    
    	
     private void eat(Fish other) {
    
    		int other_size = other.getSize();
    		size = size + other_size;
    	   
    		
    	}

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: help initialization

    Access rights (private, protected, package or public) are set on the class level, so on Fish can freely set/get the member variables of another Fish; i.e.

    Java Code:
    int other_size= other.size;
    other.size= 0;
    size= size+other_size;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: help initialization

    thanks alot.!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: help initialization

    Quote Originally Posted by NoobieCode View Post
    thanks alot.!
    You're welcome; some folks find it intrusive to fiddle-diddle with another object's member variables directy; if you're one of them you should implement a setSize( ... ) method in the Fish class (it can be private) and do:

    Java Code:
    int other_size= other.getSize();
    other.setSize(0);
    size= size+other_size;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Initialization of Classes ?
    By fatabass in forum New To Java
    Replies: 5
    Last Post: 01-28-2012, 06:25 PM
  2. Initialization
    By Sven in forum New To Java
    Replies: 4
    Last Post: 12-09-2008, 05:29 AM
  3. variable initialization
    By rqal.10 in forum New To Java
    Replies: 5
    Last Post: 06-15-2008, 06:39 AM
  4. 2D Array Initialization
    By M77 in forum Advanced Java
    Replies: 3
    Last Post: 06-04-2008, 02:12 PM
  5. log4j initialization
    By arfatkhan in forum Web Frameworks
    Replies: 3
    Last Post: 08-10-2007, 07:42 PM

Tags for this Thread

Posting Permissions

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