关于静态、非静态的相互调用包括:
1.静态的方法调用静态的方法、静态的变量 非静态的方法、非静态的变量
2.非静态的方法调用静态的方法、静态的变量 非静态的方法、非静态的变量
测试代码如下:
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><pre name="code" class="java"><pre name="code" class="java"><span style="font-size:24px;">public class Hello {
private static int stavar; //静态变量
private int unstavar; //非静态变量
private static void stamethod()//静态方法
{
stavar = 2;
// unstavar = 2; 调用不能 说明静态方法不能存取非静态变量
stamethod2();
// unstamethod(); 调用不能 说明静态方法不能调用非静态的方法
}
private void unstamethod()//非静态方法
{
stavar = 1;
unstavar = 1;
stamethod();
unstamethod2();
}
private void unstamethod2()//非静态方法
{}
private static void stamethod2()//静态方法
{}
public static void main(String[] args) //静态方法
{
}
}
</span>
总结
- 非静态的方法能调用和存取所有方法和变量。
- 静态的方法不能调用非静态变量、不能存取非静态方法;只能存取静态变量、调用静态方法。