static {table[0] = 1;}
若有多个实例,只被执行一次!
若有多个实例,只被执行一次!
解决方案 »
- 自定义泛型类,出现的错误,cannot create a generic array of E,其中E是泛型变量
- 求教如何用图片表示一个JButton
- 请教一个关于JAVA调用linux脚本意外中断的问题
- java反射:怎么取出一个类import了什么,怎么取出某属性的javadoc?
- 和大家聊下,有关数据库方面的
- Swing有没有WebBrowser控件可用,并且可以用此控件调用Web的DOM接口?
- 正则表达式 用右边替换左边
- 一个日期格式转换的问题??
- 那边有Java socket和C socket通讯的资料?我在传输数据的时候,数据不对。
- 难道weblogic6.0的性能真的是如此之烂??
- 问大家几个很简单的关于数组的问题, 望赐教!
- 哪位大哥可以整理一下javamail的有关问题吗,我愿意以分相赠
Ordinarily, when you create a class you are describing how objects of that class look and how they will behave. You don’t actually get anything until you create an object of that class with new, and at that point data storage is created and methods become available. But there are two situations in which this approach is not sufficient. One is if you want to have only one piece of storage for a particular piece of data, regardless of how many objects are created, or even if no objects are created. The other is if you need a method that isn’t associated with any particular object of this class. That is, you need a method that you can call even if no objects are created. You can achieve both of these effects with the static keyword. When you say something is static, it means that data or method is not tied to any particular object instance of that class. So even if you’ve never created an object of that class you can call a static method or access a piece of static data. With ordinary, non-static data and methods you must create an object and use that object to access the data or method, since non-static data and methods must know the particular object they are working with. Of course, since static methods don’t need any objects to be created before they are used, they cannot directly access non-static members or methods by simply calling those other members without referring to a named object (since non-static members and methods must be tied to a particular object). Some object-oriented languages use the terms class data and class methods, meaning that the data and methods exist only for the class as a whole, and not for any particular objects of the class. Sometimes the Java literature uses these terms too. To make a data member or method static, you simply place the keyword before the definition. For example, the following produces a static data member and initializes it: class StaticTest {
static int i = 47;
}
Now even if you make two StaticTest objects, there will still be only one piece of storage for StaticTest.i. Both objects will share the same i. Consider: StaticTest st1 = new StaticTest();
StaticTest st2 = new StaticTest();
At this point, both st1.i and st2.i have the same value of 47 since they refer to the same piece of memory. There are two ways to refer to a static variable. As indicated above, you can name it via an object, by saying, for example, st2.i. You can also refer to it directly through its class name, something you cannot do with a non-static member. (This is the preferred way to refer to a static variable since it emphasizes that variable’s static nature.) StaticTest.i++;
The ++ operator increments the variable. At this point, both st1.i and st2.i will have the value 48. Similar logic applies to static methods. You can refer to a static method either through an object as you can with any method, or with the special additional syntax ClassName.method( ). You define a static method in a similar way:class StaticFun {
static void incr() { StaticTest.i++; }
}
You can see that the StaticFun method incr( ) increments the static data i. You can call incr( ) in the typical way, through an object: StaticFun sf = new StaticFun();
sf.incr();Or, because incr( ) is a static method, you can call it directly through its class:StaticFun.incr();
While static, when applied to a data member, definitely changes the way the data is created (one for each class vs. the non-static one for each object), when applied to a method it’s not so dramatic. An important use of static for methods is to allow you to call that method without creating an object. This is essential, as we will see, in defining the main( ) method that is the entry point for running an application.Like any method, a static method can create or use named objects of its type, so a static method is often used as a “shepherd” for a flock of instances of its own type.