• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

best way to get a static property from a Class

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Campbell
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amirtharaj Chinnaraj:
thanks Campbell
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic