I am currently 4 months into an internship, and when reviewing my code, my boss didn't like that I had kept a specific object local to a number of methods across a few separate classes within one assembly. He didn't like that I was created a new object each time, and instead told me to create a single object that can be accessed from anywhere. I have therefore had to create it as a static object within a static class, and simply reference it from here I want to use it!

How would you deal with this, as I have only been programming professionally for 4 months!

  • 4
    Is your boss a technical person or non-technical person? If he's technical, has he read "Clean Code" by Robert C. Martin? – George Stocker Jan 19 '11 at 13:41
  • 5
    So, your boss hired you to code, and is now telling you how to code? – Jeremy Heiler Jan 19 '11 at 14:25
  • 8
    You said in a comment below that "the object doesn't have any state, apart from a lot of constants". In that case, a static class seems like a good solution to me. – Reinstate Monica Jan 19 '11 at 14:49
  • 5
    "Yes Boss" springs to mind... – user1249 Jan 19 '11 at 17:02
  • 2
    "told me to create a single object that can be accessed from anywhere. I have therefore had to create it as a static object " This isn't valid reasoning. You speak as if having a single object accessible from an entire application implied you had a Singleton -- why don't you just create it in the aggregation root and then pass its reference to the dependent classes? – devoured elysium Oct 22 '11 at 3:40

If one object is enough, creating an object every time is a waste, and here your boss may be right.

The problem is proper access to that object. A factory-like method with proper visibility which always returns that static object is the first solution that springs to mind. Others certainly exist.

  • 3
    +1: as much as i dislike any "GoF said so" answer, right is right. – Javier Jan 19 '11 at 14:06
  • 1
    I did think about Singletons, however I just saw it as a wrapper around a global variable, so I didn't follow it up. I will give this a try now, as the object doesn't have any state, apart from a lot of constants so should be fine. Thanks. – Darren Young Jan 19 '11 at 14:27
  • 9
    I suspected this might be the case ("object doesn't have any state... a lot of constants"). Your boss is right. There's no sense in putting artificial pressure on the garbage collector for values that never change. – Berin Loritsch Jan 19 '11 at 14:55
  • 3
    @darren-young: on a side note, having no state is interesting and allows for some powerful approaches; take a look at so called 'functional programming' if you didn't yet. – 9000 Jan 19 '11 at 15:09

I can't comment your specific case, but using global variables is sometimes a good solution. Java's System class is full of static, global variables.

But in general, if you think you're right and your boss is wrong, then why don't you ask your boss to explain why he thinks a global variable is a better solution? A plain "I don't like it" is as vague as "global variables are evil". You must demand more from your boss!

If he can't give reasons for his stance, and you can give reasons to your stance, or vice versa, then it's a good learning opportunity. If both of you can justify your stances, then it's probably a matter of taste - or matter of experience: your boss must have been programming professionally more than 4 months, I think?

  • 1
    See, java.lang.System contains exactly three global variables, and for each of them there's a method to properly set it. But System is full of static methods which return global objects, and there's exactly nothing wrong with this, can't argue. – 9000 Jan 19 '11 at 13:29
  • 2
    Yes; I consider global variables pretty much same as static methods that return global objects. – Joonas Pulakka Jan 19 '11 at 13:30
  • So Java does this. It’s a different question entirely of whether that’s a good idea. C’s standard random number generator keeps global state. But it’s generally acknowledged that this is horrible, and that the random number generator is therefore unusable in certain applications (see e.g. Bentley et al., Engineering a sort function). Java in particular has a host of design problems (that have been partly improved upon in C#) so I’t be very wary of using it as an argument like this. – Konrad Rudolph Jan 19 '11 at 17:32
  • @Konrald Rudolph: In the end it's a design decision. Every decision has pros and cons. For example, keeping global state in System simplifies, say, 99.9 % of use cases at the cost of causing problems in the remaining 0.1 %. There's no "right" answer to whether or not that 0.1 % is worth the extra complexity. – Joonas Pulakka Jan 19 '11 at 19:01

As someone in your position, the most you can do is learn. If the object is essentially a constant (i.e. it can't change and it doesn't maintain state), then your boss may be right. There is no harm in having a static constant object. After all, how many definitions of PI are there?

Some people defend the "no global objects" mindset to the point of religious ferver. That's for one of two reasons: they've been bit by difficult to track bugs or system fragility due to overuse of global variables/objects, or they've heard that it's bad and they can't think for themselves. I personally fall into the group of people that have been hit hard by fragility and hard to track bugs--but I've also learned some balance here.

If I were in your shoes, I'd go ahead and do what the boss says--it's his butt on the line after all. Then I'd watch and see the effects of that choice over time. This is an effective learning tool.

  • It's an internship. You're supposed to be learning. Blindly doing what your boss says isn't learning. – David Thornley Jan 19 '11 at 14:42
  • 5
    Nor is blindly going against your boss. My advice, as it was written, was to observe the effects of the boss' choice. We (on stackexchange) can't make a value judgement on the situation because we are missing vital information. This is a case to do a little scientific method. The OP has a hypothesis, boss has challenged it. Now it's time to perform the experiment. – Berin Loritsch Jan 19 '11 at 14:52

Depending on the specific situation, a global variable might be the best solution. In embedded programming, globals are not occupying stack space and for that reason a good choice.

Globals or not, I have found it a good praxis to put a prefix on globals. This will make other programmers googling your code aware of the impact if the mess with it.


Maybe what your boss wanted was you to use the Singleton Pattern. Its a very good practice when you need to access a certain object from multiple instances.

Here is a link to the wikipedias explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.