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

  • Home
  • SEARCH
  • 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 8177911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:38:20+00:00 2026-06-06T23:38:20+00:00

I need to share 1 instance between several classes. The Connect class has methods

  • 0

I need to share 1 instance between several classes.
The Connect class has methods to create a URL and to download data, and the ui is the interface (swing form) through which I get the data to build the url (dates theat comprise the url).

What’s the best way to do it?

Thought of:

1) Making it global by:

public class Global {

public static  Connect c;
}

2) Making the instance in main(), and passing it through objects.

public static void main(String[] args) throws IOException {

           Connect c = new Connect();   // get url to download from
           ui form = new ui(c);         // the form to get data from 
.
.
.

What seems more reasonable, if any?

Thank you.

  • 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-06T23:38:21+00:00Added an answer on June 6, 2026 at 11:38 pm

    The preferred way is usually to pass the instance into the constructor of whatever class needs it. That way, it’s not a guess whether or not someone has set up Global.c and when it should be available to use. It also clearly documents that each class requires a Connect object. Another benefit is that if your code ever changes and you want your UI class to no longer rely on the same Global Connect instance, you no longer have to change all your code that pulls a magic instance from this global context, and you can just pass in a different object.

    Do a search for something along the lines of “why are global variables bad” or “why are singleton accessors bad” and you’ll get a lot more detail than my explanation. There are uses for them, but if you can avoid them up front you may save some headache down the line. As with everything, there are trade-offs for each.

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

Sidebar

Related Questions

I need to share the same NSMenu instance between multiple popup buttons. When I
What is a good way to share an instance of an object between several
I need to share an instance of an object between two static objects as
I have written a C++ class that I need to share an instance of
I need to share a stack of strings between processes (possibly more complex objects
I have a few different applications that i need to share code between to
For testing purposes, I need to share some definitions between Tcl and C. Is
Imagine an implementation of the IDisposable interface, that has some public methods. If an
Is it possible to share Objects between C# applications without serialization? I need three
Our application has a need to share an existing database table with another locally

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.