public class Test {
private int count;
public Test(){
count++;
System.out.println("产生了"+count+"个对象");
}
public static void main(String arg[]){
new Test();
new Test();
System.out.print(new Test().count);
}
}
结果为:
产生了1个对象
产生了1个对象
产生了1个对象
1
为什么count不变呢?如果给count定义为static,则正确输出!
菜鸟迷糊中
解决方案 »
- Editor表情显示问题
- 关于电子口岸xml报文设计
- XML:用org.w3c.dom取xml文档的节点属性值,如何做到?
- Hibernate日志文件放在哪里?
- 我在使用了TOMCAT中自带的连接池后,使用JDBC中的事务管理会运行错误。请问怎么处理多连接和多数据源的事务处理。谢谢!
- jboss中的JAAS问题 principalRoles=null
- 请问:在网上哪儿有最新的SYBASE的JDBC,谢谢!!
- jb7开发EJB前是否还是要先装BAS?
- 请教dom的问题
- URL中问号后的Attribute传值失败有什么原因?
- Hibernate 如何使用annotation 生成uuid
- 求助!java 数据类型转换问题!!!
·
它们仅能调用其他的static 方法。
·
它们只能访问static数据。
·
它们不能以任何方式引用this 或super(关键字super 与继承有关,在下一章中描述)。
如果你需要通过计算来初始化你的static变量,你可以声明一个static块,Static 块仅在该类被加载时执行一次。下面的例子显示的类有一个static方法,一些static变量,以及一个static 初始化块:
// Demonstrate static variables,methods,and blocks. class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}static {
System.out.println("Static block initialized.");
b = a * 4;
} public static void main(String args[]) {
meth(42);
}
}
一旦UseStatic 类被装载,所有的static语句被运行。首先,a被设置为3,接着static 块执行(打印一条消息),最后,b被初始化为a*4 或12。然后调用main(),main() 调用meth() ,把值42传递给x。3个println ( ) 语句引用两个static变量a和b,以及局部变量x 。注意:在一个static 方法中引用任何实例变量都是非法的。下面是该程序的输出:Static block initialized.
x = 42
a = 3
b = 12
在定义它们的类的外面,static 方法和变量能独立于任何对象而被使用。这样,你只要在类的名字后面加点号运算符即可。例如,如果你希望从类外面调用一个static方法,你可以使用下面通用的格式:classname.method( ) 这里,classname 是类的名字,在该类中定义static方法。可以看到,这种格式与通过对象引用变量调用非static方法的格式类似。一个static变量可以以同样的格式来访问——类名加点号运算符。这就是Java 如何实现全局功能和全局变量的一个控制版本。下面是一个例子。在main() 中,static方法callme() 和static 变量b在它们的类之外被访问。class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
} 下面是该程序的输出:a = 42
b = 99
只是这个new Test()匿名对象的count属性,当然是1 ,其他的两个匿名对象的count也是属于各自的,也各自都是1。你将count定义为static的,那这个就是属于这个类的,所有的类实例(对象)共享这一个属性, 结果是3
非static属性,每次new对象,都会执行
private static int count; //属于所有实例
如果是static关键字定义出来的,则这个变量属于类,也就是不管你定义了多少个对象,这些对象拥有的这个变量都是这么一个变量。更深入的说法是,static和非static定义出来的变量占用的类空间和对象空间是不一样的。
count++是针对但前的对象private static int count;
这样属性归类本身的属性, 所有对象都用这一个变量,所以++才会递增.另外每天回帖即可获得10分可用分!
//本例演示一个很经典的jvm加载分配
class Sing{
private static Sing sing = new Sing();
public static int counter1;
public static int counter2=0;
private Sing(){
counter1++;
counter2++;
}
public static Sing getSingInstance(){
return sing;
}
}
public class MyTest {
public static void main(String[] args) {
Sing sing = Sing.getSingInstance();
System.out.println("counter1="+sing.counter1);
System.out.println("counter2="+sing.counter2);
}}
//
class Sing{
public static int counter1;
public static int counter2=0;
private static Sing sing = new Sing();
private Sing(){
counter1++;
counter2++;
}
public static Sing getSingInstance(){
return sing;
}
}
public class MyTest {
public static void main(String[] args) {
Sing sing = Sing.getSingInstance();
System.out.println("counter1="+sing.counter1);
System.out.println("counter2="+sing.counter2);
}}
如:Test.count++; 或Test t = new Test(); t.count++;
非static成员 是必须由类对象来操作的,每次new Test()时都生成一个新的存储区域,也就是3个count成员而不是一个.
如:Test t = new Test(); t.count++;
而带有static的是属于类变量.
类变量为所有实例所共有.