Topic: 对象的调用,为什么一定要使用静态的成员变量

  Print this page

1.对象的调用,为什么一定要使用静态的成员变量 Copy to clipboard
Posted by: athen
Posted on: 2005-04-29 20:57

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

    X a=new X();
    a.put(x);
    //System.out.println(x);
  }
}
编译运行没有问题
但是如果把static int x=12;的static 去掉就编译通过不了 请问各位这是什么原因,是不是静态函数调用的问题

2.Re:对象的调用,为什么一定要使用静态的成员变量 [Re: athen] Copy to clipboard
Posted by: bluepure
Posted on: 2005-04-30 00:40

System.out.println(a.x);

仔细理解成员变量的定义,不要随心所欲的写程序。

3.Re:对象的调用,为什么一定要使用静态的成员变量 [Re: athen] Copy to clipboard
Posted by: lancelob
Posted on: 2005-04-30 09:26

因为main是静态方法

4.Re:对象的调用,为什么一定要使用静态的成员变量 [Re: athen] Copy to clipboard
Posted by: vicious
Posted on: 2005-04-30 14:01

静态成员变量可以不需要初始化就能调用

5.Re:对象的调用,为什么一定要使用静态的成员变量 [Re: athen] Copy to clipboard
Posted by: andyforcjsdn
Posted on: 2005-05-01 02:46

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

6.Re:对象的调用,为什么一定要使用静态的成员变量 [Re: athen] Copy to clipboard
Posted by: jinguizi
Posted on: 2005-05-01 18:50

static类型的变量是类变量,全局都可以调用

如果x不是static类型的(比如private int x=12),那么就要创建对象才能使用x,在main函数中,a.put(X.x); a.put(a.x);这样都能打印出x

7.Re:对象的调用,为什么一定要使用静态的成员变量 [Re: athen] Copy to clipboard
Posted by: wan9721
Posted on: 2005-05-11 16:53

static int x=12;
这是定义一个静态的变量.也就是一类变量.不用实例一个对象也可以调用.
a.putFight;
这条语句中就可以直接调用这个类变量.

去掉static .int 就是一个成员变更.
只有实例一个对象.才能通过(对象名加上"."加上变更名)才可以调用.
改成:
a.put(a.x);
就可以

8.Re:对象的调用,为什么一定要使用静态的成员变量 [Re: athen] Copy to clipboard
Posted by: dengyinjun
Posted on: 2005-05-25 03:05

因为,静态的方法不能调用,非静态的成员变量,
又因为,main()是一个静态的方法,所以X要申明为静态的成员变量


   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