比如定义了一个类,一个静态变量,两个静态方法
public final class ConnectionPoolInit {
    private static String test = null;    public static String getTest(){
        return test;
    }    public static void setTest(String test){
        test = test;
    }
}在一个servlet中赋值
public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ConnectionPoolInit.setTest("abcd");
    }
}在另一个servlet中取值
public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String test = null;
        test = ConnectionPoolInit.getTest();
        System.out.println(test);
    }
}为什么总是返回空呢

解决方案 »

  1.   

    又重新做了实验,没有错误
    public static void setTest(String test){
      test = test;
      }
    赋值有问题,这样变成自己给自己赋值了,参数名称改成test1就好了。
      

  2.   

     test = test;
    改成
     ConnectionPoolInit.test =test
      

  3.   

    这样复杂肯定报错 private static String test = null;
    是私有属性。应该这样:先调用ConnectionPoolInit.setTest(test);
      

  4.   

    类的内部方法,给类的私有属性赋值不需要set方法。虽然说的不对,但是你很热心还是要谢谢你