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 terminate from a method that returns a string
how can we add (long,String) in arraylist?
Use MBTiles from online storage with OSMDroid
Loading the model from the library jar file in java
Application Force stop on GenyMotion
Error creating tabs
Error occurs when encrypting text in JTextField [duplicate]
JPA Id property and Relationship property
Using Comparator to sort HashMap in java? [duplicate]
e.getX(); does not work accurately
BigQuery - How to set read timeout in the Java client library
How to stop Java Swing JOptionPane OK_CANCEL_OPTION from closing on OK option?
Sporadic Stack Overflow error in java Matcher
When Intermediate Mapper Output files will be deleted, as soon as Mapper finishes or start of Reducer
What is Spring equivalent of a Grails “plugin” (modularizing Spring applications)
Put Image View above Button - Android

Categories

HOME
deployment
coq
deezer
single-sign-on
homebrew
raspberry-pi
syntax
sharepoint-designer
mouse
append
esper
adobe-analytics
wheelnav.js
orchardcms
fortumo
windows-server-2012
pythonanywhere
grails3
paging
wkwebview
mmap
buck
saas
wtx
webkitspeechrecognition
libssl
primitive
srcset
unboundid
skia
media-queries
host
simplexml
xcode-extension
dynamic-reports
vao
react-scripts
hendrix
jlink
alphabet
svmlight
diagnostics
font-size
gcsfuse
blacklist
angular-strap
nxt
azure-virtual-network
gridview-sorting
pyke
tcpserver
reactive-banana
execute
cartesian-product
riak-cs
vstest.console.exe
skos
javax.mail
fscommand
oxwall
jscript.net
tt-news
phalanger
mdt
rdtsc
dealloc
gridfs
ril
padarn
comaddin
robotics-studio
sublist
dice
venn-diagram
osql
spring-portlet-mvc
gwt-ext
php-parser
genshi
yetanotherforum
appender
remember-me
mozilla-prism
routedevent
uimenucontroller
phonon
wsdl.exe
misv

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App