Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've heard so many different opinions, and haven't really been able to find a solid answer to this question. When/why are static variables bad?
If I wanted to store names of players in a game in a List, would it be better to make it non-static than static? If so, why?
The list would be updated everytime a player leaves/joins. It would be used to show the players who else is playing and to track their play time.
Static variables aren't necessarily bad, but a major principle of software design is that you should contain information to the smallest context that needs to know it. If it really makes sense for a variable to be shared among all instances of a class--logger objects are a common example--then making it static is just fine. If it would ever make sense to have more than one version or copy of it, as it would in the case of the players of a game (think multiple games running simultaneously), then it's best to place the list in the smallest possible (non-static) context.
Static variables make it more difficult to see the data as the input/output of functions, because any function has access to a static. It becomes an issue when you get thousands or tens-of-thousands lines of code. Not so much in your first small app.
It's also easier to "leak memory" because the life of a static is the same as the program, meaning it never gets garbage collected till the pogram closes, or it's nulled by code.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need a hint on an Interview question that I came across. I tried to find a solution but I need advice from experts over here. What are the different strategies you would employ had you came across this particular situation? The question and my thoughts are as follows:
Q. You want to store a huge number of objects in a list in java. The number of objects is very huge and gradually increasing, but you have very limited memory available. How would you do that?
A. I answered by saying that, once the number of elements in the list
get over a certain threshold, I would dump them to a file. I would typically then build cache-like data-structure that would hold the most-frequently or recently added elements. I gave an analogy of page swapping employed by the OS.
Q. But this would involve disk access and it would be slower and affect the execution.
I did not know the solution for this and could not think properly during the interview. I tried to answer as:
A. In this case, I would think of horizontally scaling the system or
adding more RAM.
Immediately after I answered this, my telephonic interview ended. I felt that the interviewer was not happy with the answer. But then, what should have been the answer.
Moreover, I am not curious only about the answer, I would like to learn about different ways with which this can be handled.
Maybe I am not sure but It indicates that somewhat Flyweight Pattern. This is the same pattern which is been used in String pool and its efficient implementation is must Apart from that, we need focus on database related tasks in order to persist the data if the threshold limit is surpassed. Another technique is to serialize it but as you said, the interviewer was not satisfied and wanted some other explanation.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently my program is filled with many ugly references that often make field or method access look like this: weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2. I want to move to shorter and faster strong references like field1.field2. But my program design is such that I will also have to go for an ArrayList element-by-element search (in a for-loop) instead of accessing a WeakHashMap by get() method.
Thus, I'd like to understand, can moving to simpler references compensate for rejecting HashMap performance wise. Herewith I presume that WeakHashMap.get() is much faster than a loop-search of ArrayList.
Can someone, please, give me a rough estimate? Or maybe there's even an appropriate comparison table like this one. I'd appreciate that.
Thank you.
Currently my program is filled with many ugly references that often make field or method access look like this:
weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2
Given that the objects involved are not Data Transfer Objects
this is a violation of the law of Demeter aka Don't talk to Strangers / Tell, don't ask!
Following this LoD principle you should move the operations working with the data in field2 to a new method in the class SomeCustomObject.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Is it a bad practice to open resources such as database connection etc in static field of a class.
class ClassName {
static {
//open database connection
}
}
Note: I am not using the same resource in multiple classes. It's used in atmost one class only.
I am doing this, basically to ensure that the program does not fail mid away through, due to error in resource allocation.
Edit : I am new to application development(trainee). I have read most of the previous answers and could not find anything related to the question i have asked (good practices).
This is definitely bad practice.
Keep in mind that static is an abnormality to good OO design (in the end, static translates to procedural programming in disguise).
In other words: you don't want something so essential as preparing your database setup happening in some static initialisier. Meaning: most likely you should add certain abstractions to your classes do not depend directly upon such code.
Beyond that: what happens if you figure at one point that you need certain levels of control? That will be almost impossible if those "resources" are statically owned by your classes.
And of course: if your classes rely on static code ... you render them "hard" (well, almost impossible) to unit-test by default. Not using static is like lesson number one in "how to create testable code" school.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I recently started a project in Java, that contains a class called System. This class (Luckily) contains methods for output management, so in the rare cases where I need to use the System. methods (Or the System object in general) I just reference it as java.lang.System.. I believe that this could be looked down upon, as System could be looked at as a reserved name. I currently am in the beginning stages of this program, and could change it accordingly quickly, as there are little calls to the class itself.
While it's not illegal, you don't want to do this. If I were the next person working on your code, the first thing I would do is try to remove "java.lang" from "java.lang.System" and then get miffed when it wouldn't compile.
The idea is to go toward brevity and only write what you need to write, while making sense of it all for the next person. It's more an art than a science.
You could always name it something like ProjectnamehereSystem or OutputManager or something to that effect.
I would not create something so similarly named as an important class. While everything is easy to edit, you may be able to keep up with all the changes you are making.
But when the project evolves things will get messy and complex. I would suggest naming it something else that can be easily distinguished.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a Utils class that loads a large list of Strings (a static variable) and defines a function that uses this list.
I use this function in another class Solution. Currently, I am calling Utils.my_function every time I use it (in a big for loop, so it is called thousands times). Would it be faster if I instantiate a Utils in Solution? (would the list of words defined in Utils be defined only one time?)
a large list of Strings (a static variable)
...
would the list of words defined in Utils be defined only one time?
By definition, a static variable is loaded only once. So it's already the fastest you can do.
EDIT : the devs who code Java are smart. It's very likely that the JVM can detect your array is accessed very often, and will optimise its operations, whether it is a static or instance variable. However I cannot give you more information than this, and maybe some Java experts can give you a more accurate answer.
Well, as already pointed out that static is only called once anyway. Another thing you can do is batch processing -- http://java.dzone.com/articles/batch-processing-best
Instead of looping by each line -- call a set of lines at a time then perform your functions, then move to the next set, etc. You would need to profile your app to see how many lines would yield a good response for the app.