This was my interview question.
"If your team member writes code with lots of static variables and static methods, will it cause any side effects?"
can any one explain me?
This was my interview question.
"If your team member writes code with lots of static variables and static methods, will it cause any side effects?"
can any one explain me?
in case of multi threading application
static can cause problems of invalid values if two processes try to change the value at same time
it can be solved using synchronized but it will make the application slower
It very much depends on whether the static variables and methods are used for the right reasons. Static variables can either be used to record a class's state or, if declared final, for constants associated with the class.
In my experience static methods are normally utility methods which do not require access to instances of the class, or, for example, factory methods which generate instances.
The question is too broad. But i think the answer is yes.
Static code have a lot of drawbacks starting from tight coupling to possibility of making not thread safe code (such as shared mutable object).
I'd suggest to stick to best practices like SOLID. There is also an easy way to avoid static code if you are using dependency injection tools like spring or google guice.
As I understand the interviewer, the question isn't "are static variables a bad idea?" per se, just whether they lead to side effects. There's a difference. Strictly speaking a "side effect" in computer science isn't really the same thing as "an undesirable or unexpected outcome or byproduct" (although people sometimes - incorrectly, in my opinion - use it to mean that).
Think about the definition of a side effect. From the Wikipedia article on the topic:
In computer science, a function or expression is said to have a side effect if it modifies some state or has an observable interaction with calling functions or the outside world. For example, a particular function might modify a global variable or static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, or call other side-effecting functions. (Emphasis mine).
So basically, a side effect is a modification to some aspect of the program, global, or machine state that persists beyond the method call in question. (This is opposed to, for example, a pure function, which doesn't affect or depend on global state).
Thus, by definition, having a static variable doesn't cause a side effect - the act of creating global mutable state is a side effect. Side effects aren't a bad thing per se; a lot of the useful things we can do with computers (like GUIs and databases) either are (or involve) side effects. The bigger problem with public static variables (and maybe this is what the interviewer was getting at) is that they're basically global variables, which are justly considered harmful.
Static methods aren't very object-oriented. If you're doing a lot of it you're basically writing imperative code on top of an object-oriented system. With that said, though, a static method is definitely not the same thing as having side effects. A static method could be a pure function just as much as a method associated with an instance could. (Having lots of static methods is still a bad practice though, even if it's not the same as having side effects).
For small Program it will not be big deal
When Program get bigger then
You should read this too