static class
Hello
at the start of my programme I would like to store some valuse (the values are trhe user rights that will be read from the data base) these values will never change during the execution of the programme.
so I was told that the best is to create a static class ...
can any one post an example of static class and how to store the values inside
and how to read it back
or just any link that speak about this
thank you in advance.
http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html
rym82a at 2007-7-10 11:36:48 >

Hi,
I think you don't need a static class but rather a singleton class (only one instance is allowed).
Here an example of Singleton class:
public class Example {
//your attributes here
//[b]Finally[/b] your Singleton attribute here
private static final Example instance = new Example();
private Example() { //[b]PRIVATE [/b]CONSTRUCTOR
//do db access and fields/attributes initialization here
}
public static synchronized Example getInstance(){
return (instance ==null)?new Example():instance;
}
}
can you explain return (instance ==null)?new Example():instance;in particular new Example():instancewhy not use new Example()
sorry but still not clear
private static final Example instance = new Example();
what does this code mean here
private Example() { //[b]PRIVATE [/b]CONSTRUCTOR
//do db access and fields/attributes initialization here
}
this I understand
public static synchronized Example getInstance(){
return (instance ==null)?new Example():instance;
}
I did not get this ?
In case you don't understand the "lazy" style of if statement, let me explain it
return (instance ==null)?new Example():instance;
is actually identical to
if (instance ==null)
return new Example();
else
return instance;
And the code is actually checking if an instance has been initialized or not
Yes, take it.
No, initialize and take it.
rym82a at 2007-7-10 11:36:48 >

> can you explain
>
> return (instance ==null)?new Example():instance;
>
> in particular
>
> new Example():instance
>
> why not use
>
> new Example()
the exact point of the Singleton pattern (clue's in the name) is to ensure there is only one instance of the class. for example, if there is one set of user details to be used application-wide. creating a new one each time negates that
yeah i understand the ?:;my question was on the form of the new
> yeah i understand the ?:;
>
> my question was on the form of the new
the code checks to see if an instance of Example already exists, and if it does, it returns that. if not, it creates a new one first. theoretically, only one instance will ever exist, but in practice, this variant of the pattern isn't thread-safe. simplest singleton:
public class MySingleton {
private static MySingleton INSTANCE = new MySingleton();
private MySingleton() {}
public static MySingleton getInstance() {
return INSTANCE;
}
}
still lazily-loaded, despite what people might tell you, since the class itself is only loaded when you first need it
> yeah i understand the ?:;
>
> my question was on the form of the new
A private constructor without parameters and do nothing inside.
Private because it wanted to be called by getInstance() only.
Without parameters, your choice.
Do nothing inside, your choice as well.
rym82a at 2007-7-10 11:36:48 >

can I use this any where in the programme I mean do I need to pass the instance from a class to another ?
public class Example {
//your attributes here
//[b]Finally[/b] your Singleton attribute here
private static Example instance = null; //
private Example() { //[b]PRIVATE [/b]CONSTRUCTOR
//do db access and fields/attributes initialization here
}
public static synchronized Example getInstance(){
if(instance == null){
instance = new Example();
}
return instance;
}
}
You can then call getInstance when you need to create the unique instance.
> public class Example {
>
> your attributes here
>
> /[b]Finally[/b] your Singleton attribute here
>private static Example instance = null; //
>private Example() { //[b]PRIVATE [/b]CONSTRUCTOR
> //do db access and fields/attributes
> initialization here
>}
> public static synchronized Example
> getInstance(){
>if(instance == null){
> instance = new Example();
>}
> return instance;
>
>
>
> You can then call getInstance when you need to create
> the unique instance.
why bother checking instance for null in the getInstance method?
> can I use this any where in the programme > > I mean do I need to pass the instance from a class to> another ?no, objects that want to use the instance call Example.getInstance() to get the single instance
well thank you all
here is a good link about this subject
http://www.javacoffeebreak.com/articles/designpatterns/
well I have to think to whome I give the stars
hum.....
well your answer gave me only a hint to start searching ,but it diod not realy gave me the complete solution
so ... I give 1 star only ....
and the winner issssssssssss...................java_2006