我想把一个类中的static变量还原到初始状态,有什么方法吗?能不能重新装载类来做到?我试了好像没用阿for example:public class Test{
  public static void main(String[] args){
    A.i = 100;
    System.out.println(A.i);//显示 100
    
    //do something
    //我想使A.i还原为初始状态10    System.out.println(A.i);//显示 10
  }
}
class A{
  static i = 10;
}

解决方案 »

  1.   

    static变量只会初始化一次啊,类似于全局的概念,你再装载一次其实还使用原来的制。只有再用 public static final int SAVE = 10; A.i = A.SAVE 这样,再重新赋值回去。
      

  2.   

    最好的还是把实现细节隐藏起来public class A{
        private static int a = 10;
        private static final int save = a;    public int getA(){
             return a;
        }    public int setA(int a){
             this.a = a;
        }    public void revert(){
             a = save;
        }
    }
      

  3.   

    靠...疵了...setA没返回值....sorry...
      

  4.   

    只有这种方法吗?我有一个类有几十个static变量,都要这么做吗,有没有别的方法阿?一次性解决的,比如从内存中卸载class,再重新装载之类的
      

  5.   

    楼主最好能说说你要解决的问题是什么。也许你的问题不一定要通过“使static变量还原到刚刚装载class时的状态”来解决,因为这实在不是一个“常规”的方法。
      

  6.   

    是这样,有一个应用程序,原先只支持单用户,因此所有的用户配置信息都存储为一个class的static字段,现在需要增加用户登陆,注销功能,支持多用户,那么当用户注销后,所有的用户配置信息应该还原为初始状态。
      

  7.   

    静态的值只会有一个,无认你new 出多少个。
      

  8.   

    写个配置文件xml,properties,pref,都可以,配置文件里面把这些常量的初始值保存。
    以后每次都load就可以了。
      

  9.   

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping > <class name="package.Record" table="RECORDS" dynamic-insert="true" dynamic-update="true">
    <id name="id" column="ID" type="int">
    <generator class="native"/>
    </id> <discriminator column="RECORD_TYPE" type="String"/>
            <subclass name="package.ImportRecord" discriminator-value="im"/>
            <subclass name="package.ExportRecord" discriminator-value="ex"/>
            <subclass name="package.BackToProducerRecord" discriminator-value="btp"/>
            <subclass name="package.BackFromCustomerRecord" discriminator-value="bfc"/> <property name="price"  column="PRICE"  type="double" not-null="true"/>
    <property name="amount"  column="AMOUNT"  type="int" not-null="true"/> <many-to-one name="product" column="PRODUCT_ID" class="package.Product" not-null="true"/>
            <many-to-one name="order"   column="ORDER_ID"   class="package.Order"   not-null="true"/>
    </class>
    </hibernate-mapping>
      

  10.   

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping > <class name="package.Payment" table="PAYMENTS" dynamic-insert="true" dynamic-update="true">
    <id name="id" column="ID" type="int">
    <generator class="native"/>
    </id> <discriminator column="PAYMENT_TYPE" type="String"/>
            <subclass name="package.Pay" discriminator-value="p"/>
            <subclass name="package.Receive" discriminator-value="r"/> <property name="date"  column="DATE"  type="date"   not-null="true"/>
    <property name="money" column="MONEY" type="double" not-null="true"/>
            <property name="re" column="REMARK" type="String" not-null="false"/> <many-to-one name="entity" column="ENTITY_ID" class="package.Entity" not-null="true"/>
    </class>
    </hibernate-mapping>
      

  11.   

    public List<Product> getProductList();
        
        public List<Order> getOrderList(int type);
        
        public List<Order> getOrderListByEntity(int type, Long entityId);
        
        public List<Record> getRecordList(int type);
        
        public List<Record> getRecordListByProduct(int type, Long productId);
        
        public List<Record> getRecordListByOrder(int type, Long orderId);
        
        public List<Entity> getEntityList(int type);
       
        public void addProduct(Product product);
        
        public void updateProduct(Product product);
        
        public void deleteProduct(Long productId);
        
        public Product getProduct(Long productId);
        
        public void addRecord(Record record);
        
        public void deleteRecord(Long recordId);
        
        public void addOrder(Order order);
        
        public void deleteOrder(Long orderId);
        
        public void updateOrder(Order order);
        
        public void addEntity(Entity entity);
        
        public void deleteEntity(Long entityId);