anw
04-01-2007, 11:24 PM
I have the following class:
public class user extends Object
{
// Flags
public class UserFlag
{
public UserFlag(int flg, String nm, String desc)
{
flag= flg;
name= nm;
description= desc;
}
public int flag;
public String name;
public String description;
};
public static final UserFlag[] userFlags=
{
new UserFlag(0x00000001, "smsntfy", "Notify me of private messages with an SMS message"),
new UserFlag(0x00000002, "emlntfy", "Notify me of private messages with an email message"),
};
public static final int NUM_FLAGS= 2;
public static int getNamedFlag(String name)
{
for (int i= 0; i < NUM_FLAGS; i++)
{
if (name.equals(userFlags[i].name))
return userFlags[i].flag;
}
return 0;
}
public user()
{
userKey= "";
fname= "";
lname= "";
-- etc.--
}
--etc.--
}
The object is to have this embedded class of flags and their descriptions to be common and immutable for all instantiantions of the user class.
I get an error that says:
/home/anw/SoftwareProducts/WebWork/infoisland/src/infoIsland/user.java:40: non-static variable this cannot be referenced from a static context
new UserFlag(0x00000001, "smsntfy", "Notify me of private messages with an SMS message"),
^
when I try to do this.
I think it doesn't like the "new" keyword. Having said that, I've tried several combinations and permutations of syntax to try to achieve this (no "new' keyword, no "UserFlags" so it's like an array initialization, etc.).
The goal is to have the same stuff for each user, and, for reasons of efficiency, I'd like to not to have separate instances of this for each user. What is the Java way to do this?
TIA
public class user extends Object
{
// Flags
public class UserFlag
{
public UserFlag(int flg, String nm, String desc)
{
flag= flg;
name= nm;
description= desc;
}
public int flag;
public String name;
public String description;
};
public static final UserFlag[] userFlags=
{
new UserFlag(0x00000001, "smsntfy", "Notify me of private messages with an SMS message"),
new UserFlag(0x00000002, "emlntfy", "Notify me of private messages with an email message"),
};
public static final int NUM_FLAGS= 2;
public static int getNamedFlag(String name)
{
for (int i= 0; i < NUM_FLAGS; i++)
{
if (name.equals(userFlags[i].name))
return userFlags[i].flag;
}
return 0;
}
public user()
{
userKey= "";
fname= "";
lname= "";
-- etc.--
}
--etc.--
}
The object is to have this embedded class of flags and their descriptions to be common and immutable for all instantiantions of the user class.
I get an error that says:
/home/anw/SoftwareProducts/WebWork/infoisland/src/infoIsland/user.java:40: non-static variable this cannot be referenced from a static context
new UserFlag(0x00000001, "smsntfy", "Notify me of private messages with an SMS message"),
^
when I try to do this.
I think it doesn't like the "new" keyword. Having said that, I've tried several combinations and permutations of syntax to try to achieve this (no "new' keyword, no "UserFlags" so it's like an array initialization, etc.).
The goal is to have the same stuff for each user, and, for reasons of efficiency, I'd like to not to have separate instances of this for each user. What is the Java way to do this?
TIA