我是一名Java程序员,对企业界不熟悉.最近我使用Groovy和Java 开发了一个应用程序.我编写的所有代码都使用了相当多的静态代码.高级技术部门要求我减少使用的静力学数量.我用Google搜索了同样的东西,我发现很多程序员都反对使用静态变量.
我发现静态变量使用起来更方便.而且我认为它们也是有效的(如果我错了,请纠正我),因为如果我必须对一个类中的函数进行10,000次调用,我很乐意将该方法设置为静态并使用简单的方法Class.methodCall()而不是用10,000个类的实例来混乱内存,对吧?
此外,静态减少了代码其他部分的相互依赖性.他们可以充当完美的国家持有者.除此之外,我发现静态在一些语言中得到广泛实现,如Smalltalk和Scala.那么为什么程序员(尤其是Java世界)中普遍存在对静态的压迫呢?
PS:如果我对静力学的假设是错误的,请纠正我.
我正在开发C#和asp.net Web应用程序.
我有一个叫做实用程序的通用类,我在这个公共实用程序类中有很多公共和静态变量.
由于这个数字逐渐增加,我想知道将实用程序方法和变量存储为公共静态是一种好的做法.
我的代码示例
public class utilities
{
public static string utilVariable1 = "Myvalue";
public static string utilVariable2 = "Myvalue";
public static string utilVariable3 = "Myvalue";
:
public static string utilVariableN = "Myvalue";
public static string UtilMethod1()
{
//do something
}
public static string UtilMethod2()
{
//do something
}
public static string UtilMethodN()
{
//do something
}
}
Run Code Online (Sandbox Code Playgroud) 我正在android中创建一个小型的musicplayer应用程序,其中我的主要活动是使用许多这样的静态变量: -
public class Fragmentactivity extends FragmentActivity {
static ArrayList<String> s3=new ArrayList<String>();
static ArrayList<String> s5=new ArrayList<String>();
static ArrayList<String> songpaths=new ArrayList<String>();
static ArrayList<String> alldirectorypaths=new ArrayList<String>();
static ArrayList<String> internalpaths=new ArrayList<String>();
static ArrayList<String> tempsongnames=new ArrayList<String>();
static ArrayList<Bitmap> thumbnails=new ArrayList<Bitmap>();
static int internal=0;
static ArrayList<String> folderpaths=new ArrayList<String>();
//my addtional code after it
}
Run Code Online (Sandbox Code Playgroud)
我正在其他类中访问它,如: -
public class A{
Fragmentactivity.songpaths.get(index);
//and in many other classes also i used it
Run Code Online (Sandbox Code Playgroud)
在我的大多数课程中,我已经制作了许多静态变量但是现在我了解到这不是开发的好习惯.
我应该继续使用静态还是应该使用getter/setter?
或者,如果有任何其他方式重用变量...请建议我.
任何帮助,将不胜感激 :)
提前致谢 :)