Topic: how to change var in method??(with parameter )

  Print this page

1.how to change var in method??(with parameter ) Copy to clipboard
Posted by: eliuhy
Posted on: 2003-03-11 19:51

public class TT {
  //
  static int i = 2;
  


  
    public void gg(int a){
    //
  
    a++;
    
  }
//
public static void main(String[] args){
  
TT app = new TT();

  System.out.println(app.i);
  //
  

  //
  app.gg(app.i);
  System.out.println(app.i);
  //

  System.exit(0);
}
}
//

please help me!

2.Re:how to change var in method?? [Re: eliuhy] Copy to clipboard
Posted by: cmslovehxh
Posted on: 2003-03-11 21:23

static var can only change by static method!you can try it like this:
public class TT {
//
static int i = 2;



public static void gg(){
//

i++;

}
//
public static void main(String[] args){

TT app = new TT();

System.out.println(app.i);
//


//
app.gg();
System.out.println(app.i);
//

System.exit(0);
}
}
//

3.Re:how to change var in method?? [Re: eliuhy] Copy to clipboard
Posted by: frankwg
Posted on: 2003-03-11 21:41

static variable can be changed by both static and non-static method.
The problem can not change the parameter value is due to the parameter passing is by value not by reference.

Thus, in the code, you just changed the value of parameter 'a' in calling stack, not the passed variable.

4.Re:how to change var in method?? [Re: eliuhy] Copy to clipboard
Posted by: cmslovehxh
Posted on: 2003-03-11 21:51

ow!i am sorry!i am wrong!static method can only change static variable!butstatic variable can be changed by both static and non-static method

5.Re:how to change var in method?? [Re: eliuhy] Copy to clipboard
Posted by: eliuhy
Posted on: 2003-03-11 21:55

多谢!,
我是想通过参数!

public class Swap{
  //
  private Integer a = new Integer(10);
  private Integer aa = new Integer(20);
  
  //
  
  public void tt(Integer b){
  Integer aa = new Integer(20);
    b = aa;
    System.out.println(b);
  }
  

  

  public static void main(String[] args){
    //
    Swap app = new Swap();
    System.out.println(app.a);
//
System.out.println(app.a);
  
    System.exit(0);
  }
}

6.Re:how to change var in method?? [Re: eliuhy] Copy to clipboard
Posted by: shaneangel
Posted on: 2003-03-11 22:52

oh,my god!

ur code is very mess, please clean it up,OK?

Do ur purpose want to exchang two variables?


   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