java


Why delegate when loading classes in java


As the javase 7 documentation describes
The ClassLoader class uses a delegation model to search for classes
and resources. Each instance of ClassLoader has an associated parent
class loader. When requested to find a class or resource, a
ClassLoader instance will delegate the search for the class or
resource to its parent class loader before attempting to find the
class or resource itself.
Why ClassLoader delegate the search for the class or resource to its parent? What is the purpose or the advantage of doing so?
There are several valid reasons for classloading delegation. I have listed them according to their priority(from my understanding):
Security
There are certain classes in Java which shouldn't be messed with. By having parent-first delegation model, JVM can be sure that it is executing only those classes and not the one loaded by custom classloaders.
Avoid duplicate class instances
Classloading is a costly operation as it requires reading data from external storage or network, parsing the bytecode, allocating memory and so on. So restricting the JVM to load the classes only once is one of the reasons. By forming classloader hierarchy with the parent-first rule will achieve this.
Class Scope
There are certain classes which are the core part of Java, like java.lang.*. These classes are part of Java language and will be used at almost all the places. Since a class is identified uniquely by its fully qualified name along with the classloader which loaded that class, it is important to have a single classloader to load such classes. Thus the bootstrap and extension classloader take care of this.
Also, by loading the classes and resources at top level gives them a broader scope than loading at the bottom of the classloader hierarchy.

Related Links

how to make below jdbc connection configuration connection pooling
Creating one Node in Java changes the rest of them
Java :“error: cannot find symbol”
Meaning of #Nullable java (android)
How to protect auto-generated sources during clean package in maven?
android.database.sqlite.SQLiteException: near “;”: syntax error (code 1)
Unreachable code with multiple catch statements
java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z
TreeMap node is null after being assigned
Disable #EnableAutoConfiguration
Not able to move to next formatted text field using java selenium Webdriver
Calling a Jersey API from a remote location
How a single object is showing different value for the same field (Inheritance) [duplicate]
Hibernate PersistentList has data, but returns null
WSDL wsimport lower upper case issue
Run a single java package on eclipse using maven

Categories

HOME
ionic-framework
google-play
cplex
jsrender
rdf
cakephp-2.5
twitter-bootstrap-4
angular-ui-bootstrap
multiple-records
contact
synchronization
cloudkit
kibana-4
mapserver
postgres-xl
ibm-odm
circuit
highlight.js
saxon
reactive-cocoa
reverse-proxy
autoconf
extjs5
lcd
hammerspoon
h2db
javacv
facet
entitlements
typo3-6.2.x
elasticsearch-net
compatibility
atlassian-crucible
unboundid
preg-match
bcd
ecto
scorm2004
mmenu
environment-modules
dosbox
xcode-extension
space-complexity
dynamics-crm-2013
occlusion
reportbuilder
ansible-playbook
elasticsearch-plugin
greenrobot-eventbus
android-tabhost
nomethoderror
auto-update
chain-builder
jedis
dds
jquery-nestable
python-webbrowser
return-value
veracode
fancybox-2
composite-key
font-size
np-complete
spring-cache
gnome-shell-extensions
google-cdn
medium.com
libpng
bonobo
sts-springsourcetoolsuite
underscore.js-templating
paypal-express
contact-list
angstrom-linux
nsviewcontroller
ipconfig
browser-link
historian
cyclomatic-complexity
pundit
security-testing
browser-bugs
deis
winddk
runtime.exec
dbconnection
html4
android-hardware
gil
cisco-jtapi
pendrive
assembly-loading
qt-jambi
xmlspy
servicehost
asp.net-mvc-areas
sef
javap
xetex
thread-local-storage
mediarss
scripting-languages
w3wp
wsdl.exe
misv

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App