0

I want to share One common variable inside my Swing Application throughout the application life cycle. It should be same Application Context in Servlets .What are the ways i can achieve it?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kuldeep
  • 13
  • 1
  • 3
  • 1
    Your question is a bit broad and smells a bad programming practice. Consider providing a bit more context as to what you are trying to achieve. Providing code and explaining where you are stuck will highly improve the chance of getting a valuable answer. – Guillaume Polet Apr 15 '13 at 09:14

1 Answers1

1

You could use some kind of singleton, even though I would never recommend singletons in general.

You can also use an IOC container in your Swing application or pass references yourself as needed.

Like @Guillaume said, it doesn't look good in terms of design, but you have options.

You could also use static variables and refactor the code as soon as you understand a bit more the implications of your original design approach, and before the code base grows significantly.

When you start introducing quickly many global objects without rethinking your design, it can introduce issues that you'll not be able to fix for many reasons:

  • Too much code and too much tight coupling
  • Weird concurrency issues depending on what your code does
  • Lack of flexibility if someday you need many instances of those global objects.
rimero
  • 2,383
  • 1
  • 14
  • 8