<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nilotpal Mrinal on Medium]]></title>
        <description><![CDATA[Stories by Nilotpal Mrinal on Medium]]></description>
        <link>https://medium.com/@nilotpal.mrinal.97?source=rss-c252f117df10------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BzIPMYomOmEDf-ew</url>
            <title>Stories by Nilotpal Mrinal on Medium</title>
            <link>https://medium.com/@nilotpal.mrinal.97?source=rss-c252f117df10------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 20:00:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nilotpal.mrinal.97/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Nested Classes —  Why you should use them]]></title>
            <link>https://medium.com/@nilotpal.mrinal.97/nested-classes-why-you-should-use-them-fc942e487031?source=rss-c252f117df10------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc942e487031</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Nilotpal Mrinal]]></dc:creator>
            <pubDate>Sun, 16 Oct 2022 04:34:26 GMT</pubDate>
            <atom:updated>2022-10-17T01:49:40.409Z</atom:updated>
            <content:encoded><![CDATA[<h3>Nested Classes — Why you should use them</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Iv_zJkrY3tgAiLGaQc98XQ.jpeg" /><figcaption>“Java is C++ without the guns, clubs and knives.” ~ James Gosling</figcaption></figure><h3>Introduction</h3><p>A typical class in java consists of variable declarations, constructors and methods. But many a times, classes can have interfaces and other classes too. A class declared within another class is called a nested class. Similarly an interface declared within another interface or class is called a nested interface. In this article, we will take a look at nested classes, when we should use them and how they can improve our code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*j5kobvWCp-ZFM69Reyfl3A.png" /><figcaption>Java Class</figcaption></figure><h3>Why do nested classes exist ?</h3><p>You can always create a top level class and use that instead of a nested class. Therefore it’s very easy to question the existence of nested classes. <br>Various java libraries use nested classes heavily. So there must be some very good reason for their existence.</p><p>Consider a scenario where you need to use some class X within another class Y . The simplest way to do this is to create a top level class X and use it in class Y. That works but what if no other class needs X? X may not even have a significance of its own. X may have a meaning only in context of Y. For example, let’s take Node class in a HashMap . Node objects serve the purpose of storing mapping of the HashMap. Node’s usage makes sense only in the context of HashMap. Therefore it is sensible to keep the Node class within the HashMap class itself . This is the very essence of nested classes. Nested classes are meant to serve the enclosing classes.</p><h3>Benefits of using Nested Classes</h3><p>There are many benefits of using nested classes. Some of them are:</p><p>1. Prevents Class Pollution — Since nested classes are declared within other classes, we end up creating a very small number of top level classes, thus preventing class pollution.</p><p>2. Functionality is kept closer to the original class.</p><p>3. Better readability and design — The reader of the code can associate the nested class with the enclosing class</p><h3>Types of Nested Classes</h3><p>Broadly speaking , nested classes can be categorised into two types. These are:</p><h4>1. Inner Classes</h4><p>These are used when the nested class needs access to the outer class’s state. Since the inner class object needs access to the outer class’s object, it is necessary that the outer class’s object is created prior to creation of inner class’s object and the inner object has a way of accessing the outer object. For this, the inner class object maintains a hidden reference to the outer object. Since the inner object has to build a reference to outer object, the creation of inner object is a heavy operation. That also implies that as long as the inner object is alive, the outer object cannot be garbage collected, as the inner object will always have a reference to outer object.</p><p>The above also implies that inner classes cannot have static members. The reason is very simple: To access any static member of the inner class, you would first need to create an object of it ( cos inner class is not static) and then use that object to access any static member, which goes against the definition of static which means something that is independent of objects.<br>Common use cases of inner classes include Adapters which are used to provide different views of an object’s state.</p><p>Inner classes can be categorised into 3 main types<br>1. Non Static member class — Simple non static inner classes which acts as a member of the enclosing class. They are simply instance member of the enclosing class.</p><p>2. Anonymous classes — These are nameless classes which are declared and instantiated at the point of use. They are not a member of class but are used within a method ( static or non static method of the enclosing class). When declared within instance method, anonymous classes can access the state of the enclosing outer object but when used in a static context, they cannot use the enclosing object’s state for the simple reason that the static method in which an anonymous class is declared may not even require an object for invocation. The most common use cases of anonymous classes is to use them as a Function objects or as strategy. For example, we can pass an anonymous class object as a comparator for sorting. Anonymous classes cannot implement interfaces or extends other classes.</p><p>3. Local Classes — These are classes which are declared within methods similar to anonymous classes but these have a name and can even extend other classes and implement interfaces. The usage of these classes are very rare.</p><h4>2. Static Member Classes</h4><p>Static member classes, as the names suggests, are those nested classes which are static in nature. Since they are static, they do not require an outer object for their object’s creation. They are used when there is no requirement for the inner class’s object to access enclosing class’s state. Since there is no need for inner object to access outer object, there is no need to maintain a hidden reference and thus creation of static member classes are very light weight.<br>Static members are like static members of the enclosing class and like any static member, cannot access the instance fields and methods of enclosing class.</p><p>Static member classes are just like any outer class but is nested due to the fundamental reason that their existence makes sense within outer class.<br>Since static member class is just like any other top level outer class, they can have both static and non static members.</p><p>Common use cases of static member classes are:<br>1. Represent private components of enclosing objects — The outer class can create objects of the nested static class for their purposes</p><p>2.Represent strategies — Anonymous classes are good for strategies but they have the limitation that they cannot extend multiple classes/ implement interfaces. Static member classes can overcome all such limitations.</p><p>3. Public Helper Class — The static member class can provide a bunch of utility methods which can be used by the objects of enclosing class.</p><h3>Visibility of Nested Classes</h3><p>Nested classes can have all 4 levels of access. i.e private, public, default and protected. This is different from top level classes which can have either public or default access level.</p><h3>Compilation of Nested Classes</h3><p>Nested classes when compiled produces class files for both outer and inner classes. The outer class file will simply be named as Outer.class while the inner classes will generate Outer$NestedClassName.class file. In case of anonymous classes , the generated class files will look like Outer$1.class, Outer$2.class and so on as they do not have any name.</p><h3>Accessibility of members</h3><p>There is mutual accessibility of members including private i.e the outer class can access inner class’s members and inner class can access outer class’s members. Note that these accessibility is constrained by the context i.e you cannot access instance member from static context. Otherwise there is mutual accessibility.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc942e487031" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Method Binding — How JVM Does It !]]></title>
            <link>https://medium.com/@nilotpal.mrinal.97/method-binding-how-jvm-does-it-d23a8e733f0a?source=rss-c252f117df10------2</link>
            <guid isPermaLink="false">https://medium.com/p/d23a8e733f0a</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java8-tutorial]]></category>
            <dc:creator><![CDATA[Nilotpal Mrinal]]></dc:creator>
            <pubDate>Sat, 15 Oct 2022 05:52:01 GMT</pubDate>
            <atom:updated>2022-10-15T06:58:34.056Z</atom:updated>
            <content:encoded><![CDATA[<h3>Method Binding — How JVM Does It !</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xe3B-7WwndvOazik90P0oA.png" /></figure><p>Methods are at the heart of any programming language. No matter where you are in your programming journey, you would find yourselves working with methods regularly. From a simple “Hello World” program to a highly complex system , all consists of various objects interacting with each other via methods. Therefore it is very essential to understand how the method binding process works.</p><blockquote>What is Method Binding ?</blockquote><p>Method binding is the process of binding a method call to its declaration and implementation. Confused ? In simple terms, whenever there is a method call ( be it a static or instance method), the JVM needs to know which method implementation to use during runtime.</p><blockquote>Why is it needed?</blockquote><p>Java provides a very powerful mechanism called Polymorphism. Polymorphism makes it possible to pass a subclass object whenever a superclass object is expected. This allows changing the behaviour at runtime without changing the code. The code can continue using the superclass reference and the caller can pass a subclass object with overridden methods at runtime.A subclass may override the methods in the superclass for a number of reasons. Some of these include changing the behaviour of the method, replacing a bad implementation with a good one etc.</p><p>Since a superclass reference can be passed any subclass object at runtime, it is necessary for the JVM to decide which specific overridden implementation to use during runtime.</p><h3>The Method Binding Process</h3><p>Method binding is a 2 step process. These steps are:</p><h4>1. Signature Binding</h4><p>When we call a method on an object , the compiler uses the reference type of that object to decide if that method call is allowed or not. For example , if the reference is of type Animal , no matter what is the actual object this reference points to , we can only call the methods which are there in Animal class.</p><p>For every method call , be it a static or instance method, the compiler writes the signature details in the byte code at compile time. The details consists of return type, method name and the method parameters. These details are based on the reference type used for invocation.</p><h4>2. Implementation Binding</h4><p>Once the signature binding happens, the JVM uses the signature to decide which specific implementation to use during runtime.</p><blockquote><em>Note that the implementation binding needs to happen only for instance methods. Static methods and fields ( both instance and static) are binded during compile time itself based on the reference type. If there is a call to a static method on a reference, no matter which class’s object the reference holds during runtime, the method called will be the static method in the class of the reference type. Same goes for fields i.e the field value is used based on the reference type. If there is no reference type used to call the static method/ reference the field, that method/ field to be used is the one which exists in the current context or higher up the class hierarchy.</em></blockquote><blockquote><em>The decision of which static method to call / field to use happens at compile time itself.</em></blockquote><p>For instance methods however , the JVM uses the actual object type to decide which method implementation to use during runtime. JVM starts scanning from the most specific class and goes up the class hierarchy to find an exact signature match ( Note that this signature is written into byte code as a part of step1). Going up the class hierarchy, the first implementation which matches the signature is used. The rules of signature matching is very simple : <br>1. The method name should be exactly same as written in byte code<br>2. The method parameters should be exactly same as written in byte code.<br>3. The return type should be compatible — i.e the return type should be either the same as the return type in byte code or any subclass of it.</p><p>Note that this process of climbing up the class hierarchy happens every time an instance method invocation is encountered. So if for some invocation, the JVM climbed up the class hierarchy and there it encounters another instance method invocation, it will again start climbing from the most specific class to find the correct method to use.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d23a8e733f0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>