Click to See Complete Forum and Search --> : What does 'class' do in annexed code fragment?


Wanton Killer
September 5th, 2001, 02:02 AM
Hi,
I've been using this peice of code that I got off an example for some time now but I really don't understand what it means. Want some information about what exactly the 'class' part in the 'DBUtil.class.getRe...' section of the following code fragment means.


JDBCProperties = new Properties();
FileInputStream fisProperty = new FileInputStream((DBUtil.class.getResource("JDBC.properties")).getFile());
JDBCProperties.load(fisProperty);





Thanks in Advance

Cheers!

Wanton killer

dlorde
September 5th, 2001, 05:04 AM
Every Java class has an object associated with it that describes it's data and methods and provides various generic utilities. This object is an instance of the Class class in the java.lang package (see the JavaDocs for 'Class'). To access this Class object, you can treat it as if it was a static member of your class called 'class'. Sorry if it's a bit confusing, but I didn't decide on the naming. The Class class has a getResource() method that will use the classloader of its 'owner' class to find some associated resource.

In this case, the 'class' object (an instance of Class) that describes DBUtil is being used to find the resource called "JDBC.properties". It will delegate this task to the class loader of DBUtil.

As I understand it, these two statements will return the same class object:Class fooClass1 = Class.forName("Foo");
Class fooClass2 = Foo.class;

Dave

To email me remove '_spamjam' from my email address

Wanton Killer
September 5th, 2001, 07:34 AM
Thanks dude!

That was very valuable information

Cheers!

Wanton killer

dlorde
September 5th, 2001, 07:37 AM
Glad to help :-)

Dave

To email me remove '_spamjam' from my email address