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 37603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:34:35+00:00 2026-05-10T14:34:35+00:00

In Delphi, I want to be able to create an private object that’s associated

  • 0

In Delphi, I want to be able to create an private object that’s associated with a class, and access it from all instances of that class. In Java, I’d use:

public class MyObject {     private static final MySharedObject mySharedObjectInstance = new MySharedObject(); } 

Or, if MySharedObject needed more complicated initialization, in Java I could instantiate and initialize it in a static initializer block.

(You might have guessed… I know my Java but I’m rather new to Delphi…)

Anyway, I don’t want to instantiate a new MySharedObject each time I create an instance of MyObject, but I do want a MySharedObject to be accessible from each instance of MyObject. (It’s actually logging that has spurred me to try to figure this out – I’m using Log4D and I want to store a TLogLogger as a class variable for each class that has logging functionality.)

What’s the neatest way to do something like this in Delphi?

  • 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. 2026-05-10T14:34:36+00:00Added an answer on May 10, 2026 at 2:34 pm

    Here is how I’ll do that using a class variable, a class procedure and an initialization block:

    unit MyObject;  interface  type  TMyObject = class    private      class var FLogger : TLogLogger;    public      class procedure SetLogger(value:TLogLogger);      class procedure FreeLogger;    end;  implementation  class procedure TMyObject.SetLogger(value:TLogLogger); begin   // sanity checks here   FLogger := Value; end;  class procedure TMyObject.FreeLogger; begin   if assigned(FLogger) then      FLogger.Free; end;  initialization   TMyObject.SetLogger(TLogLogger.Create); finalization   TMyObject.FreeLogger; end. 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I want to be able to parse Javascript source in a Delphi Application.
I`m using Delphi 2009 and want to operate some XML data. I heard that
We want migrate from Delphi 7.0 to Delphi 2009, to stay up with a
I use Ant to build Delphi applications (called from CruiseControl). I want ant to
HI, Im using Delphi and I want to make an application that can do
When debugging I always want Delphi to open my application in the second monitor
I'm using Delphi 6, and I want a database bound list box with multiselect.
I'm using standard Delphi constants DayMonday, etc and I want to convert them to
I am using a popup menu in Delphi. I want to use it in
I use Indy 9 with Delphi 5. In my application I want to communicate

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.