{"id":4845,"date":"2024-02-10T15:36:17","date_gmt":"2024-02-10T15:36:17","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4845"},"modified":"2024-02-11T07:31:24","modified_gmt":"2024-02-11T07:31:24","slug":"all-about-class-loading-and-initialization","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/all-about-class-loading-and-initialization\/","title":{"rendered":"All about Class Loading and initialization"},"content":{"rendered":"<p><strong class=\"ctaHeader2\">What is Class Loading?<\/strong><br \/>\nLoading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation.<\/p>\n<p><strong class=\"ctaHeader2\">When Class is Loaded?<\/strong><\/p>\n<ol>\n<li>Class loading done by Class Loaders may eagerly load a class or does lazy load<\/li>\n<li>Eager loading happens as soon as another class references it and lazy load happens when a need of class initialization occurs.<\/li>\n<li>If a Class is loaded before it&#8217;s actually being used, it can sit inside before being initialized which may vary from JVM to JVM. <\/li>\n<li>However, it&#8217;s guaranteed by JLS that a class will be loaded when there is a need of static initialization.<\/li>\n<\/ol>\n<p>Whenever the JVM determines it needs a class (to use its static variables, to create a new object, to use its static methods etc) it will load the class and static initialization blocks will run, static variables are initialized etc. This is (at least under normal circumstances) done only once<\/p>\n<pre>\r\nSomeClass.someStaticMethod(); \/\/SomeClass is loaded (if its not already)\r\nSomeClass.someStaticVariable; \/\/SomeClass is loaded (if its not already)\r\nSomeClass var=new SomeClass(); \/\/SomeClass is BOTH loaded (if its not already) AND instantiated\r\n<\/pre>\n<p><em>Class loader actually loads byte code into JVM , runs static initializers when you want to use static fields within class without creating an instance of it, class must be loaded by class loader first<\/em>.Default classloader in java is java.lang.ClassLoader<\/p>\n<p><em>Class Loading is mostly unpredictable<\/em><\/p>\n<p><strong class=\"ctaHeader2\">When Class is Initialized?<\/strong><br \/>\nClass Initialization happens after class loading. A class is initialized in Java when<\/p>\n<ol>\n<li>An Instance of class is created using either <strong>new() keyword<\/strong> or using reflection using <strong>class.forName()<\/strong>, which may throw ClassNotFoundException in Java.<\/li>\n<li>an static method of Class is invoked.<\/li>\n<li>an static field of Class is assigned.<\/li>\n<li> an static field of class is used which is not a constant variable.<\/li>\n<li>if Class is a top level class and an assert statement lexically nested within class is<\/li>\n<\/ol>\n<p><strong class=\"ctaHeader2\">Which classes are Eagerly Loaded and Lazily Loaded<\/strong><br \/>\n<strong><br \/>\nEagerly Loaded Classes<\/strong> are those the JVM must be able to load JVM class files. The JVM class loader loads referenced JVM classes that have not already been linked to the runtime system. Classes are loaded implicitly because: The initial class file &#8211; the class file containing the public static void main(String args[]) method &#8211; must be loaded at startup. Depending on the class policy adopted by the JVM, classes referenced by this initial class can be loaded in either a lazy or eager manner.<\/p>\n<p>An eager class loader loads all the classes comprising the application code at startup. Lazy class loaders wait until the first active use of a class before loading and linking its class file.<\/p>\n<p>The first active use of a class occurs when one of the following occurs: \u2022 An instance of that class is created \u2022 An instance of one of its subclasses is initialized \u2022 One of its static fields is initialized. Certain classes, such as ClassNotFoundException, are loaded implicitly by the JVM to support execution. You may also load classes explicitly using the java.lang.Class.forName() method in the Java API, or through the creation of a user class loader.<\/p>\n<p>Based on the above rules, lazy class loading can be forced on the system.<\/p>\n<pre>\r\nUse java -XX:+TraceClassLoading to trace the loading of classes.\r\n\r\nUse java -XX:+PrintCompilation to trace when methods are JITed.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is Class Loading? Loading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation. When Class is Loaded? Class loading done by Class Loaders may eagerly load a class or does lazy load Eager loading happens&hellip; <a href=\"https:\/\/codethataint.com\/blog\/all-about-class-loading-and-initialization\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-4845","post","type-post","status-publish","format-standard","hentry","category-classes-and-objects"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4845"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4845\/revisions"}],"predecessor-version":[{"id":4855,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4845\/revisions\/4855"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}