Java: Assigning Non Static Variable to Static Variable -
i'm using class called globalvar
store global variables required entire application. class looks this:
public class globalvar { myprops props = new myprops(); public globalvar() throws exception { } public static string ppfirstname = "kim"; public static string pplastname = "smith"; string zip = props.props.getipzip("ipzip"); public static string ipzip = zip; }
however, in last 2 lines i'm getting error:
cannot assign non static variable static variable.
i understand source of problem (as zip
non static , ipzip
static) question is: there way around this? i.e, way assign value set in "props"
"ipzip"
static var?
note: purpose of using method able use syntax globalvar.ipzip
anywhere in application..
you creating instance of myprops
, calling non-static getipzip
method. cannot assign result of non-static method static variable.
instead, need convert getipzip
method static method of myprops
class, assign result yor ipzip
variable this.
public static string ipzip = myprops.getipzip("ipzip");
then access ipzip
anywhere using globalvar.ipzip
.
however, if make getipzip
method static, adding variable globalvar
class unnecessary, call myprops.getipzip("ipzip");
anywhere in code.
another option move getipzip
method globalvar
class , make static method in there, call globalvar.getipzip("ipzip");
.
Comments
Post a Comment