静态方法中只能使用静态变量,静态方法。非静态变量,静态方法使用前先实例化。 class A { public static void main(String[] args) { System.out.println("Hello World!"); A test = new A(); //建立一个该类的实例 test.print(); //调用该实例的方法 } public void print() { System.out.println("hello!"); } } 即可。
class A { public static void main(String[] args) { A a = new A(); a.print(); } public void print() { System.out.println("hello!"); } }
to panq():static就表示所以此类型的类空间中只生成一个实例!这句不对么? 是误会了我说的吧? 是我说的不太明确!
A a = new A(); a.print(); panq()说的是对的。
static 有点像C/C++中全局变量,但还是不一样…… static 定义的变量和方法不需要生成类的实例就可以直接使用,所以改为public static void ……就可以的。 如果去掉static 则是一个实例方法,需要实例化后才能使用,于是 A a= new a(); // 对象实例化 a.print(); // 调用 搞定!
:21bird(世纪菜鸟) 是误会了
class A { public static void main(String[] args) { System.out.println("Hello World!"); A test = new A(); test.print(); } public void print() { System.out.println("hello!"); } } 是也!
两种方法: 1、方法一 class A { public static void main(String[] args) { System.out.println("Hello World!"); A test = new A(); test.print(); } public void print() { System.out.println("hello!"); } } 2、方法二 class A { public static void main(String[] args) { System.out.println("Hello World!"); print(); } public static void print() { System.out.println("hello!"); } }这下该明白了吧! ! ! !!! ! ! !! ! ! !! ! !! ! ! !! ! ! ! ! !! !
static 方法只能调用static 方法 想调用非static 方法必须创建对象!
main方法必须是static的, 其它方法也可以是 static.
这个问题哦, static 是程序的入口的概念吧...
同意:: Tarloy() static 一项重要的用途就是帮助我们在不必创建对象的前提下调用那个方法. 这一点是至关重要---特别是在定义程序运行入口方法main()的时候. 例如:你的例子可以这样改: class A { public static void main(String[] args) { System.out.println("Hello World!"); A.print(); } public static void print() { System.out.println("hello!"); } }
class A
{
public static void main(String[] args)
{
System.out.println("Hello World!");
A test = new A(); //建立一个该类的实例
test.print(); //调用该实例的方法
}
public void print()
{
System.out.println("hello!");
}
}
即可。
{
public static void main(String[] args)
{
A a = new A();
a.print();
}
public void print()
{
System.out.println("hello!");
}
}
1。方法定义在前后并无关系。
2。static方法的数目是不限的。
静态的怎么能调用非静态函数,它又不知道是哪个实例的。
不可能。
静态方法中不能直接使用非静态方法。
main就是典型的静态方法。
static方法里面只能调用static的变量,方法。你要说出道理?那你先回答我为什么1+1偏要等于2哪?
panq()说的是对的。
静态方法中只能使用静态变量,静态方法。
因为所谓静态就是类的所有实例公用一个方法,
不管你new了多少个实例,静态方法只有一个。
因此,如果你在静态方法中调用了静态变量或者静态方法的话,它没办法知道是属于哪一个实例的啊!因此就要出错!
还有,静态变量和静态方法的个数是没有限制的!
跟c++差不多的!
看来,我以前看的java全都就饭吃了!
不过,都是在学习嘛!重在搀和!@_@
说错了,不是还有大虾么!
希望大家别见怪!
是误会了我说的吧?
是我说的不太明确!
a.print();
panq()说的是对的。
static 定义的变量和方法不需要生成类的实例就可以直接使用,所以改为public static void ……就可以的。 如果去掉static 则是一个实例方法,需要实例化后才能使用,于是
A a= new a(); // 对象实例化
a.print(); // 调用
搞定!
是误会了
{
public static void main(String[] args)
{
System.out.println("Hello World!");
A test = new A();
test.print();
}
public void print()
{
System.out.println("hello!");
}
}
是也!
1、方法一
class A
{
public static void main(String[] args)
{
System.out.println("Hello World!");
A test = new A();
test.print();
}
public void print()
{
System.out.println("hello!");
}
}
2、方法二
class A
{
public static void main(String[] args)
{
System.out.println("Hello World!");
print();
}
public static void print()
{
System.out.println("hello!");
}
}这下该明白了吧!
!
!
!!!
!
!
!!
!
!
!!
!
!!
!
!
!!
!
!
!
!
!!
!
想调用非static 方法必须创建对象!
其它方法也可以是 static.
static 是程序的入口的概念吧...
static 一项重要的用途就是帮助我们在不必创建对象的前提下调用那个方法.
这一点是至关重要---特别是在定义程序运行入口方法main()的时候.
例如:你的例子可以这样改:
class A
{
public static void main(String[] args)
{
System.out.println("Hello World!");
A.print();
}
public static void print()
{
System.out.println("hello!");
}
}
static用在类上有上述效果。
用在变量上,只需对该变量初始化一次。
你要明白static方法不是属于对象的,所以在static的方法中无法调用类的方法!
您搞错啦!
这个帖子不是我发的!
如果我舍得200分的话,我的问题也许不会有那么多没解决了!^&^