Reputation: 17
I have not programmed in Java in a while, I tried to figure this out but could not.
When I declare a class variable then try to update it and print it, the variable does not change from null. Can someone tell me what I am doing wrong. Here is the code I typed up to test this. When I call the method and print it that gets the right variable but when just calling the class variable it is still set to 0. Thank you for you help
package test;
public class method {
public static int z ;
public method(){
}
public static void setZ(int z){
method.z = z;
}
public static int getZ(){
return method.z;
}
public static int add(int z){
method.z = 15;
return method.z;
}
public static void main(String[] args) {
System.out.println(z);
}
}
Upvotes: 0
Views: 2445
Reputation: 3507
you need method call before print statement
package test;
public class method {
public static int z ;
public method(){
}
public static void setZ(int z){
method.z = z;
}
public static int getZ(){
return method.z;
}
public static int add(int z){
method.z = 15;
return method.z;
}
public static void main(String[] args) {
setZ(10);
System.out.println(z);
}
}
Upvotes: 3