dcsimg
Is it possible to create static properties/methods...
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Terren_Suydam
Posted On:   Saturday, March 17, 2001 05:58 PM

Is it possible to create static properties/methods of a bean class? Does the beans spec cover this and can builder tools handle a singleton bean, for example?

Re: Is it possible to create static properties/methods...

Posted By:   Luigi_Viggiano  
Posted On:   Friday, March 30, 2001 12:03 AM

I think, getter and setter shouldn't be static, but the property "container" yes. Hope this code to be a correct JavaBean:



import java.io.*;
public class SingletonBean implements Serializable {
private static String sample;

public SingletonBean() {
}
public String getSample() {
return sample;
}
public void setSample(String newSample) {
sample = newSample;
}
}

Re: Is it possible to create static properties/methods...

Posted By:   Luigi_Viggiano  
Posted On:   Thursday, March 29, 2001 04:30 AM

Static properties/methods are not recognized to be properties/metods of a JavaBean.
To define a singleton bean you can code it as the following example:

class SimpleSingleton {
private static SimpleSingleton instance;
private String sampleProperty;

//The constructor shall be protected or private
//friendly and public is not enough
//to prevent client's instantiation.
protected SimpleSingleton() {}

public static SimpleSingleton getInstance() {
if (null == instance) {
instance = new SimpleSingleton();
}
return instance;
}

public void setSampleProperty(String newSampleProperty) {
sampleProperty = newSampleProperty;
}
public String getSampleProperty() {
return sampleProperty;
}
}

public class ExampleSingleton {

public static void main(String[] args) {
SimpleSingleton one = SimpleSingleton.getInstance();
one.setSampleProperty("Always the same!");

//SimpleSingleton anotherOne = new SimpleSingleton(); //you cannot do that!
SimpleSingleton anotherOne = SimpleSingleton.getInstance();
System.out.println("Property of another one : " +
anotherOne.getSampleProperty());
}
}

Re: Is it possible to create static properties/methods...

Posted By:   John_Zukowski  
Posted On:   Saturday, March 17, 2001 08:53 PM

There is nothing that stops you from creating static methods within a Bean class. However, they won't be treated as properties. Also not supported is creating beans from methods other than the constructor.
About | Sitemap | Contact