Results 1 to 8 of 8
  1. #1
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Question return new variable -problem

    What does it mean when a function returns a value which starts with new:
    Java Code:
       public Monkey makeChild(Monkey other){
          String childGenome;
             childGenome = other.genome;  //child gets parents' genome
          //This I do not understand
         [B] return [U]new[/U] Monkey(childGenome);[/B]
          //using a constructor which is defined below
          }
    What does the last line above mean?
    It seems that it possible to return constructors.

    The constructor which is preferenced above is:
    Java Code:
       public Monkey(String genome){
          this.identity = counter;
          ++counter;
          this.genome = genome;  // defined: public String genome
       }

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

    Default

    It's time we sit down and have a talk. When two monkeys love each other a very lot (and only after they get married), they sometimes decide to make a baby monkey. Since this baby is an entirely new object of the Monkey class, it needs to be constructed with use of the "new" keyword.

    Question though: if this is a program that's modeling genetics, don't you have to somehow mix the genome of the "other" monkey with the "this" monkey with random assortment and all those other genetic terms to create the new genome for your childMonkey? A genome that gets half of its information from "this" genome and half from the "other" genome?

  3. #3
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Correct! I have this too in my code.

    Thanks for the comment! So we must use the new word to make an entirely new object i.e. a new monkey.

  4. #4
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Now, I understand the code.

    This
    Java Code:
    return new Monkey(childGenome);
    can be written as:
    Java Code:
          Monkey child = new Monkey(childGenome);
          return child;
    In my opinion, the last two sentences are clearer than the first sentence.
    Also, the fact that an object can be made of the class inside the class itself allowed me to solve the problem. It was also new to me that you can create an object outside the main.
    Last edited by Hevonen; 12-07-2008 at 01:42 PM.

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

    Default

    Sounds like you're heading in the right direction. One quick note about this:
    So we must use the new word to make an entirely new object i.e. a new monkey.
    while using new is a very common way to create new objects, please know that there are other ways available. Using new isn't a must per se (although it probably is necessary with your program).

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

    Default

    > Using new isn't a must per se
    Outside of Reflection and Class#newInstance() / Constructor#new Instance(...) is there some other way I'm missing?

    thanks, db

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

    Default

    how about cloning?

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

Similar Threads

  1. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 11:25 AM
  2. Replies: 3
    Last Post: 11-24-2008, 07:01 PM
  3. how to use session variable in my problem
    By Arif Baig in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-27-2008, 07:23 AM
  4. Shared variable mutual exclusion problem. Plz help
    By elecrobot in forum Threads and Synchronization
    Replies: 6
    Last Post: 11-25-2007, 02:43 PM
  5. I have a problem with variable "i"
    By silvia in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 11:05 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
  •