public void firstMethod(){ int i=4,j=5; this.i=i+j; int l=this.i; System.out.println(l); secondMethod(7); } public void secondMethod(int i){ int j=8; this.i=i+j; int l=this.i; System.out.println(l); } public static void main(String args[]){ ScopeExample s=new ScopeExample(); s.firstMethod();
public static void main(String[] args)
{
System.out.println(i);
}
System.out.println(i);
^
1 error不行,我做过的
把I换成static 的就行了
两个this.i分别为多少啊?
由于i属于实例变量,所以只有类实例化为对象它才存在,所以main()应该写为:
public static void main(String[] args)
{
ScopeExample object=new ScopeExample();
System.out.println(object.i);
}
public static void main(String[] args)
{
ScopeExample object=new ScopeExample();
System.out.println(object.i);
object.firstMethod();
System.out.println("in firstMethod ,this.i="+object.i);
object.secondMethod(1);//这个参数1可以自己选择
System.out.println("in secondMethod ,this.i="+object.i);
}
private int i=1;
public void firstMethod(){
int i=4,j=5;
this.i=i+j;
int l=this.i;
System.out.println(l);
secondMethod(7);
}
public void secondMethod(int i){
int j=8;
this.i=i+j;
int l=this.i;
System.out.println(l);
}
public static void main(String args[]){
ScopeExample s=new ScopeExample();
s.firstMethod();
}
}
//不知道这样可不可以
public static void main(String[] args)
{
ScopeExample object=new ScopeExample();
System.out.println(object.i);
object.firstMethod();
System.out.println("in firstMethod ,this.i="+object.i);
object.secondMethod(object.i);//传入你要的参数
System.out.println("in secondMethod ,this.i="+object.i);
}
public static void main(String[] args)
{
ScopeExample object=new ScopeExample();
System.out.println(object.i);
object.firstMethod();
System.out.println("in firstMethod ,this.i="+object.i);
object.secondMethod(7);//类似你第1个方法的调用
System.out.println("in secondMethod ,this.i="+object.i);
}