简体   繁体   中英

How to change static variable inside in a class by a method

I have three classes:

Class One

public class One {
   private static Two object;

   public static void set_up(Two object) {
       int y = object.get();
       System.out.println(y);
   }

   public static void prn () {
       System.out.println(object.get());
   }

}  

Class Two

public class Two {
   private int x;


   public int get() {
       return x;
   }

   Two(int n){
       x = n;
   }
 }

Class Three

public class Three {
   public static void main( String[] argv ) {
       One st = new One();
       Two two = new Two(2);

       st.set_up(two);

       st.prn();
   }
}

I want to change the static variable object in class Two by method set_up(Two object) . The problem is that static variable inside the class has the same name as the arguments in the method. How can I modify set_up(Two object) so I copy values from given argument to static object?

You can qualify it by using the class' name:

public static void set_up(Two object) {
    One.object = object;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM