public class swap39{                  //定义类
    public static void main(String[] args){
        Integer a=new Integer(10);
        Integer b=new Integer(50);
        System.out.println("before swap...");
        System.out.println("a is "+a);
        System.out.println("b is "+b);
        swap(a,b);
        System.out.println("after swap...");
        System.out.println("a is "+a);
        System.out.println("b is "+b);
    }       
    public static void swap(Integer pa,Integer pb){
        Integer temp=pa;
        pa=pb;
        pb=temp;
        System.out.println("in swap...");
        System.out.println("a is "+a);
        System.out.println("b is "+b);
    }
}   

解决方案 »

  1.   

    swap(a,b); //静态方法调用......加上类名试试swap39.swap(a,b); 
      

  2.   

    怎么弄图片啊。。怎么在新浪相册上传的图片不显示啊郁闷。。编译出错信息为:
    18:cannot resolve symbol    symbol:variable a  location: class swap39  
    System.ou.println("a is "+a^);a是一个对象啊。。难道不能打印出赋值为10的a值吗。。???请指教
      

  3.   

    难道楼主是想问为什么swap后a和b为什么没有实际变化吗?如果是这样,那么,我可以很高兴的告诉楼主,函数内部对于类的类似于地址方式的改变是局部生效的,在函数执行结束时不会生效。
    类似问题在csdn提出的还是挺多的。搜搜看看?
      

  4.   

    System.out.println("a is " + a);
    System.out.println("b is " + b);这两个地方出错,没有定义
      

  5.   

    因为,你要打印的是pa和pb,不是a和b……
      

  6.   


     Integer a=new Integer(10); 
     Integer b=new Integer(50); 这不是定义和初始化赋值吗??
      

  7.   

     public static void swap(Integer pa,Integer pb){ 
            Integer temp=pa; 
            pa=pb; 
            pb=temp; 
            System.out.println("in swap..."); 
            System.out.println("a is "+a); 
            System.out.println("b is "+b); 
        } 说的是这两个地方没定义 
      

  8.   

    main()中的a,b是局部变量,不能在swap()中使用
      

  9.   

    对啊。。在swap方法中,a,b的值传给pa,pb,System.out.println("a is "+a); 这个语句不能执行啊。。因为没有a的值,只有pa的值
    +a,+b,改成+pa,+pb,已正确谢谢。。  太粗心了