wood burning stoves 2.0*
The moose likes Java in General and the fly likes Common data for one (multi-instatianted) class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Common data for one (multi-instatianted) class" Watch "Common data for one (multi-instatianted) class" New topic
Author

Common data for one (multi-instatianted) class

Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
I have the following class:


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


-------<br />Thanks & regards,<br />anw
Dave Lorde
Greenhorn

Joined: Apr 02, 2007
Posts: 20
The inner class UserFlag is declared as an instance class, so you can't instantiate it at class (static) scope.

If you want to instantiate it at static scope, you must declare it a static member class:

Never hesitate to ask a lesser person...
Confucius
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
That did it!! Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Common data for one (multi-instatianted) class
 
Similar Threads
Question in Map
Assigning a Class with Multiple Parameters
how to extend a static builder
Need help in Java Class
can static method be overloaded ? reason?