aspose file tools*
The moose likes Beginning Java and the fly likes best way to get a static property from a Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "best way to get a static property from a Class" Watch "best way to get a static property from a Class" New topic
Author

best way to get a static property from a Class

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
hi guys

what is the best way to acess a static property in a classs

lets say for example iam having the following example

public class BaseDAOimpl
{

private static Properties appComponents;

static {

try{
appComponents = AppUtils.loadProperties();

}
catch(Exception ex)
{
System.out.println(" "+ex);
}


}//end of static




public get(String k)
{
String name = BaseDAOimpl.appComponents.getProperty(k);
System.out.println(" k "+k);

}



public static Properties getAppComponents() {
return appComponents;
}


public static void setAppComponents(Properties appComponents) {
BusinessObjectFactory.appComponents = appComponents;
}


}



iam getting error(Null pointer Excetion) in the line
String name =BaseDAOimpl.appComponents.getProperty(k);

if i change this line to below shown

String name =BaseDAOimpl.getAppComponents().getProperty(k);
my program is running fine
so suggest me what is the best way to acess a static property in a class
[ August 25, 2008: Message edited by: Amirtharaj Chinnaraj ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
What's a property in a class? Not standard Java nomenclature. You mean a field. You get access to a private static field the same way you get access to a private instance field-with an accessor (get) method.

If the class has a private static field without a public [static] get method, then whoever wrote that class doesn't intend to grant you access to that field.
Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 236
thanks Campbell
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Originally posted by Amirtharaj Chinnaraj:
thanks Campbell
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: best way to get a static property from a Class