Results 1 to 6 of 6

Thread: Constructor call question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004

    Constructor call question

    I have a chunk of code in class Aquarium that looks like this:

    Code:
        public static void main(String[] args)
        {
            new Aquarium();
        }
    I know that "new Aquarium()" is used to create a new Aquarium object, but what is the scope of this new object? Why would you want to do this?

    Thanks.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Constructor call question

    If you don't want to hold a reference to the object, do that.
    for example, if you place all code necessary to work with a JFrame in it's constructor. You want need to hold a reference to it in the main program
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004

    Re: Constructor call question

    So when this constructor is called, is this object immediately destroyed or does it hang around until main is completed?
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Constructor call question

    This depends on what type is the object...
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004

    Re: Constructor call question

    This depends on what type is the object...
    I am sorry, but I don't quite know what you mean. I am sort of new to Java.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Constructor call question

    Every object created with new hangs around at least until there are no more references to it, and then some (until the GC runs and kills it). In other words, object lifetime in Java is non-deterministic: you cannot predict it exactly.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width