Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Got resources?

Got resources?

Tutorial Details:

Got resources?
Got resources?
By: By Vladimir Roubtsov
Two strategies for loading resource bundles and property files from Java code
November 22, 2002
What is the difference between
Class.getResource() and ClassLoader.getResource() ?
Because Class.getResource() eventually delegates to ClassLoader.getResource() , the two methods are indeed very similar. However, the first method is often preferable. It provides a nice extra feature: it looks up package-local resources. As an example, this code snippet
getClass().getResource("settings.properties");
executed from a class some.pkg.MyClass looks for a resource deployed as some/pkg/settings.properties . You might wonder why this is better then the equivalent
getClass().getClassLoader().getResource("some/pkg/settings.properties");
The reason is the possibility for future refactoring. Should you decide to rename pkg to betterpkgname and move all classes and resources into the new package, the first code snippet requires no further code changes. The second code snippet embeds the old package name in a string literal?something that is easy to forget and can become a runtime error later.
Another advantage of Class.getResource() is that it does not require the getClassLoader runtime security permission, which the other approach requires.
I should mention a few extra details. Is it better to acquire the relevant Class object using the getClass() instance method or using MyClass.class ? The answer depends on whether you plan to have classes in other packages extend MyClass . Since getClass() always returns the most derived class, it might return a class in a package different from some.pkg , possibly coded after MyClass is conceived. If this is a possibility, you should safeguard against potential lookup errors by using the class literal syntax form, MyClass.class . As an added benefit, it also works from static methods.
This page formated for crawlers and browsers that don't support scripts and tables.
Home
EZone


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Got resources?

View Tutorial:
Got resources?

Related Tutorials:

Results of first-ever JVM server benchmark revealed - JavaWorld - December 1997
Results of first-ever JVM server benchmark revealed - JavaWorld - December 1997
 
Java gets serial support with the new javax.comm package - JavaWorld - May 1998
Java gets serial support with the new javax.comm package - JavaWorld - May 1998
 
Java Tip 71: Use dynamic messaging in Java - JavaWorld - April 1999
Java Tip 71: Use dynamic messaging in Java - JavaWorld - April 1999
 
Java gets serial support with the new javax.comm package
Java gets serial support with the new javax.comm package
 
RMI over IIOP - JavaWorld December 1999
RMI over IIOP - JavaWorld December 1999
 
Use XML data binding to do your laundry
Use XML data binding to do your laundry
 
I want my AOP!, Part 3
I want my AOP!, Part 3
 
Got resources?
Got resources?
 
Simply Singleton
Simply Singleton
 
Smartly load your properties
Smartly load your properties
 
Good introduction
Good introduction
 
Good ideas
Good ideas
 
Tracing in a multithreaded, multiplatform environment
Tracing in a multithreaded, multiplatform environment In \"Use a consistent trace system for easier debugging,\" Scott Clee showed you how to trace and log from a custom class to provide a consistent tracing approach across your applications. This approa
 
The HTML Renderer Shootout, Part 1
The HTML Renderer Shootout, Part 1 In this article, we will review 11 different HTML renderers, comparing their features, compliance, and speed; searching for the best one for any project.
 
An example of public use
ashkelon lite "ashkelon lite" provides all the benefits of the ashkelon product without any of the hassle and overhead. It's ashkelon in a war file. There's no need to setup a database, no need to configure _anything_, not even to populate a repository
 
Clean Up Your Mess: Managing Temp Files in Java Apps
Clean Up Your Mess: Managing Temp Files in Java Apps Creating and managing temporary files in a Java application can be a little tricky due to some open JVM bugs. Develop a workaround with some custom code and a clever design.
 
JSP Escape taglib ver. 1.83
Also useful for WAP/WML developers - converts on the fly HTML to WML. Library has got two body tags Escape and WmlEscape.
 
J2J - Java to JavaScript integration.
It is a development tool lets you to integrate Java classes and JavaScript within your HTML pages. The main idea behind this product is how to call methods of Java classes right from JavaScript functions.
 
Using JConsole to Monitor Applications
JConsole is the Java Monitoring and Management Console, a new graphical tool shipped in J2SE JDK 5.0. This article describes how JConsole can be used to observe information about an application running on the Java platform, with an overview of the J2SE 5.
 
A Practical Roadmap for Deploying Enterprise Web Applications: Taking the First Step
This article covers important decision points around the availability, scalability, and security that are needed for enterprise web applications.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.