Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:对象的调用,为什么一定要使用静态的成员变量 [Re:athen]
andyforcjsdn





发贴: 1
积分: 0
于 2005-05-01 02:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Hi,

First of all, pls forgive me use english to talk to you, it is because i don't know any chinese input method. Your question is quite interesting, i think many Java newbies may face the same problem, to solve it we should understand that there are two fields in Java, one is Class Field another is Instance Field.

Definition of Class Fields:

A Class Field is associated with the class in which it is defined, rather than with an instance of the class. The key point to understand about a static field is that there is only a single copy of it. This field is associated with the class itself, not with instances of the class. (excerpt from Java in a nutshell, chapter 3, Object-Oriented Programming in Java - Class Fields).
http://www.unix.org.ua/orelly/java-ent/jnut/ch03_01.htm

So you can access class variables (fields) through an instance or through the class itself.

For example:

class X
{
static int x=12; // class field
void put(int y){
System.out.println(y);
}
public static void main(String[] args)
{

X a = new X();
System.out.println(a.x); //accessing through an instance
System.out.println(X.x); //accessing through the class itself

}
}

Definition of Instance Fields:

Instance fields are associated with instances of the class, rather than with the class itself.
Thus, every X object (instance) we create has its own copy of the int field x.
So you can access an instance member only through a reference to an instance.

For example,

class X
{
int x=12; // instance field
void put(int y){
System.out.println(y);
}
public static void main(String[] args)
{

X a = new X();
System.out.println(a.x); // only accessing through an instance
System.out.pringln(X.x); // illegal
Systme.out.println(x); // illegal
}
}

Pls also refer to http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
i hope it will be helpful for you, good luck.

andy


andyforcjsdn edited on 2005-05-01 09:26


25了才开始学JAVA晚吗?

话题树型展开
人气 标题 作者 字数 发贴时间
6663 对象的调用,为什么一定要使用静态的成员变量 athen 373 2005-04-29 20:57
5353 Re:对象的调用,为什么一定要使用静态的成员变量 bluepure 51 2005-04-30 00:40
5392 Re:对象的调用,为什么一定要使用静态的成员变量 lancelob 11 2005-04-30 09:26
5416 Re:对象的调用,为什么一定要使用静态的成员变量 vicious 18 2005-04-30 14:01
5518 Re:对象的调用,为什么一定要使用静态的成员变量 andyforcjsdn 1972 2005-05-01 02:46
5344 Re:对象的调用,为什么一定要使用静态的成员变量 jinguizi 119 2005-05-01 18:50
5399 Re:对象的调用,为什么一定要使用静态的成员变量 wan9721 169 2005-05-11 16:53
5393 Re:对象的调用,为什么一定要使用静态的成员变量 dengyinjun 57 2005-05-25 03:05

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923