Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:39:24+00:00 2026-06-10T15:39:24+00:00

Good morning. I am currently working on a Java Web Service project, that is

  • 0

Good morning.

I am currently working on a Java Web Service project, that is deployed on an Apache Tomcat 7 server. For the needs of the project, I need to maintain a global (among WS threads, created by every request) object in memory. Thus, I tried to implement a Singleton design pattern as follows:

public class SingletonDesign {
    private static boolean _instanceFlag = false;
    private static final SingletonDesign _instance = new SingletonDesign();
    private static GlobalObject _myGlobalObject;

    private SingletonDesign() {
        if(_instanceFlag == false) {
            _myGlobalObject = new GlobalObject();
            _instanceFlag = true;
        }
    }

    public static GlobalObject getModel() {
        if(_instanceFlag == false) {
            _myGlobalObject = new GlobalObject();
            _instanceFlag = true;
        }
        return _myGlobalObject;
    }

    public static int parseModel() {
        if(_instanceFlag == false) {
            _myGlobalObject = new ItemSimilarityModel();
            _instanceFlag = true;
        }
        int val = _myGlobalObject.parseFromFile();
        return val;
    }
}

I am aware of the fact that every time a request is done, a new thread would be created on my Web – service class (object). My goal, is to have a global SingletonDesign object among all threads, which stays alive in memory, and is created only once.
Despite the fact that the above design seems to be working according to my expectations, I am not really sure if it is correct. So these are my questions:

1) Why do the methods of the SingletonDesign object need to be static? (I have attempted to define them as non-static, but my Singleton object is not initialized properly).

2) The above design is seen from the Wikipedia page on the Singleton Design Pattern. The part that confuses me is the initialization of the _instance field, which I have seen in other Singleton implementations too. Why do we need that object?

3) Does my object stay alive until the server stops? I have made some tests and It seems that it stays alive, but I have to be 100% sure.

Thank you for your time and interest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:39:26+00:00Added an answer on June 10, 2026 at 3:39 pm

    The _instance field contains your singleton. You need a static getInstance() method, and your other methods would be instance methods. I don;t think you need the _instanceFlag field at all.

    Then you would call it as follows:

    SingletonDesign.getInstance().getGlobalObject();

    Still, keep in mind that a lot of people (including myself) thing of Singleton as a code-smell. Also, this design is not considered the best way to implement a Singleton in Java, cfr: What is an efficient way to implement a singleton pattern in Java?

    The object will stay alive as long as its classloader stays alive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning, I am currently developing a java web application that exposes a web
Good morning, I am currently working on a project in which I need to
Good morning, I am currently working on a project that utilises Codeigniter and the
A very good morning, i am currently working on a simple php project, the
Good morning, I am currently finishing a project: http://schaedlingsbekaempfer24.eu/ After embeding a little bit
Good morning everyone, I'm currently building a jquery step by step form at: http://jsfiddle.net/xSkgH/
Good morning gang. I have a jQuery Datepicker object that is prepoulated using a
Good Morning, I have a very odd error working in adobe flexbuilder 3. Ever
Good morning, I'm having a really annoying issue with Highcharts, such that the x-axis
Spent the good part of this morning working through this, but still…. apologies if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.