Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to call single instance of class? singlet.

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default how to call single instance of class? singlet.

    Hey question about creating a single instance of a class.
    I have two classes and the first class I would use
    CourseOfferingController cOffer= new CourseOfferingController(); but later found out its wrong since it keeps on creating a new instance. I added this to my other class
    // singleton object
    static StudentRecordController controller;
    public static StudentRecordController getController() {
    return controller;
    }


    so, not sure what I do in the first class to call it, I tried
    StudentRecordController sAdd = StudentRecordController.getController();
    but now program crashes on this line Student s = sAdd.addStudent(studentid,studentname, studentnamel);
    It worked before when I had CourseOfferingController cOffer= new CourseOfferingController(); but not now so Im guessing
    StudentRecordController sAdd = StudentRecordController.getController(); is the problem.

    thanks

    --- Update ---

    nvm solved it, had to add if(controller == null)
    {
    controller = new StudentRecordController();
    }


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to call single instance of class? singlet.

    Singleton concept is basically implemented when we require to use a single instance of a class throughout the application. For this we follow some rules.

    1. We don't allow any class to create the object of the singleton class except the class itself for which we need to make the constructor the class private.

    private StudentRecordController(){}
    2. Then again we create the object of the singleton class in the same class within a method which is your getController method but strictly checking that not more than one object is returned from that method so that whenever that method is called from any other class it always returns the same object of that class.

    public static StudentRecordController getController() {
    if (object == null) { //checks here whether the object of the class has already been made or not.
    object = new StudentRecordController();
    }
    return object;
    }
    3. To check the for the same object the reference varible we are using is "object" as mentioned above. That should be static and private so that whenever we access that particular variable it should always return the same object due to its single copy property.

    private static StudentRecordController;

Similar Threads

  1. Single Instance of a class
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: August 28th, 2012, 06:01 PM
  2. How to return class instance?
    By nera1981 in forum JDBC & Databases
    Replies: 7
    Last Post: August 23rd, 2011, 02:14 PM
  3. Replies: 2
    Last Post: January 22nd, 2011, 11:20 PM
  4. Compare instance of a class to another
    By srs in forum Java Theory & Questions
    Replies: 5
    Last Post: December 2nd, 2010, 10:39 AM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 06:20 PM