Why are static variables considered evil?
Static variables represent global state. That's hard to reason about and hard to test: if I create a new instance of an object, I can reason about its new state within tests. If I use code which is using static variables, it could be in any state - and anything could be modifying it.
I could go on for quite a while, but the bigger concept to think about is that the tighter the scope of something, the easier it is to reason about. We're good at thinking about small things, but it's hard to reason about the state of a million line system if there's no modularity. This applies to all sorts of things, by the way - not just static variables.
Why are static variables considered evil?
Static variables represent global state. That's hard to reason about and hard to test: if I create a new instance of an object, I can reason about its new state within tests. If I use code which is using static variables, it could be in any state - and anything could be modifying it.
I could go on for quite a while, but the bigger concept to think about is that the tighter the scope of something, the easier it is to reason about. We're good at thinking about small things, but it's hard to reason about the state of a million line system if there's no modularity. This applies to all sorts of things, by the way - not just static variables.
Why are static variables considered evil?
Static variables represent global state. That's hard to reason about and hard to test: if I create a new instance of an object, I can reason about its new state within tests. If I use code which is using static variables, it could be in any state - and anything could be modifying it.
I could go on for quite a while, but the bigger concept to think about is that the tighter the scope of something, the easier it is to reason about. We're good at thinking about small things, but it's hard to reason about the state of a million line system if there's no modularity. This applies to all sorts of things, by the way - not just static variables.
Why are static variables considered evil?
Static variables represent global state. That's hard to reason about and hard to test: if I create a new instance of an object, I can reason about its new state within tests. If I use code which is using static variables, it could be in any state - and anything could be modifying it.
I could go on for quite a while, but the bigger concept to think about is that the tighter the scope of something, the easier it is to reason about. We're good at thinking about small things, but it's hard to reason about the state of a million line system if there's no modularity. This applies to all sorts of things, by the way - not just static variables.
Static Variables : Good or Bad?
You can replace all you static fields with a "Context" object which you can pass around or make a Singleton. It is possible to remove almost all your static fields away. Whether this is a good idea or not is up to you, but I wouldn't assume that it has to be much harder to use instance fields.
BTW: I would suggest
- placing static fields/constant with the class or package which uses them
- treat static arrays as immutable if possible making them
final
as well.
You can use a non-static Context with
public class Context {
public static final String PREF_UTILITY_FILE_NAME = "PrefUtilityFile";
public Facebook fb;
public AsyncFacebookRunner fbAsyncRunner;
public String[] fbPermissions = {"email", "read_stream", "user_birthday"};
public SharedPreferences prefs;
public Editor editor;
public String access_token;
public long expires;
}
// pass to constructor as required
class UsesContext {
final Context context;
public UsesContext(Context context) {
this.context = context;
}
public void method() {
// can use context
}
}
This allows you to create unit tests with multiple Contexts.
The only thing I would leave static are constants.
Static Methods and static variables, bad design practise?
static mainly used for to accessing class members without creating class instance. As you know that why we are writing public static void main()
just bcoz at the run time jvm can access this method without creating the class object and start up your program.
Its depending on your requirement that how you used or say how to declaring member of class using access specifier as private, public, default and protect for access level while the static was for make that as static with access level.
Without static you must declare your member as public or if you inherit your class then make as protected also. As in the same package you define the class then just static for inheriting the class. And for using without inhert class you need to create object of that class and then you can access there members (Note it based on access specifier)
here is the link below for more detail
http://xahlee.info/java-a-day/access_specifiers.html
http://www.javabeginner.com/learn-java/introduction-to-java-access-modifiers
Related Topics
How to Clone Arraylist and Also Clone Its Contents
How to Get the Current Working Directory in Java
Why Is a Concurrentmodificationexception Thrown and How to Debug It
Streamcorruptedexception: Invalid Type Code: Ac
How to Get a File'S Media Type (Mime Type)
Why Is Subtracting These Two Times (In 1927) Giving a Strange Result
How to Set Environment Variables from Java
Error - Trustanchors Parameter Must Be Non-Empty
How Do Getters and Setters Work
Using Jfreechart to Display Recent Changes in a Time Series
What Is a Classpath and How to Set It
Why Can't I Use Switch Statement on a String
Do Java Arrays Have a Maximum Size
Why Is 128==128 False But 127==127 Is True When Comparing Integer Wrappers in Java
Why Don't Java Generics Support Primitive Types
How to Simulate the C++ 'Friend' Concept in Java