Results 1 to 8 of 8
  1. #1
    OddOne is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Make an object from a class once

    Hello everyone,

    I have a question. I have a 'Support' class that makes an object from 'Sensor' class in the constructor, like this:

    sensor = new Sensor();

    But I want to make the object from the class 'Sensor' only once. So when I make an object from the 'Support' class again I don't want it to make another object from the class 'Sensor'.

    Is this possible?

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

    Default Re: Make an object from a class once

    There is a coding technique (AKA pattern) called a Singleton that does what you want.
    Do a Google on the Singleton pattern.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    OddOne is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Make an object from a class once

    Thank you very much for the fast reply!

    I tried it, but I still get an error at the moment the new object is being made in the 'Support' class. So I thought I have to prevent it from making an new object.
    I thought of something like this:

    Initialize:
    private boolean control; // automatically set on false

    Then:
    if(control == false){
    sensor = new Sensor();
    control = true;
    }

    but once I call the class again, it sets the control to false again so it won't work. I also tried it with getters and setters to save the value of control in another class, but I have to call that class too, for calling the methods.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,893
    Rep Power
    25

    Default Re: Make an object from a class once

    That does not look like the code for the Singleton Pattern. Where did you find that code?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    OddOne is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Make an object from a class once

    That wasn't the Singleton Pattern, but something I thought of myself since I get an error when using the Singleton Pattern. The error occurs when the object is being made, so I thought I have to prevent it.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,893
    Rep Power
    25

    Default Re: Make an object from a class once

    I get an error when using the Singleton Pattern
    Post the code and the error message if you want help with it.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Make an object from a class once

    Quote Originally Posted by OddOne View Post
    That wasn't the Singleton Pattern, but something I thought of myself since I get an error when using the Singleton Pattern. The error occurs when the object is being made, so I thought I have to prevent it.
    Read all about patterns on this site; it explains about the use of the patterns and includes example code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    OddOne is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Make an object from a class once

    Thank you very much Norm and Jos. I tried the Singleton pattern and I don't get the error anymore.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 05:37 AM
  2. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  3. Deleted Class template code. Now I cant make class.
    By AcousticBruce in forum IntelliJ IDEA
    Replies: 0
    Last Post: 01-11-2011, 11:52 PM
  4. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  5. Replies: 3
    Last Post: 03-15-2010, 09:44 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
  •