<?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 rakibul rushel on Medium]]></title>
        <description><![CDATA[Stories by rakibul rushel on Medium]]></description>
        <link>https://medium.com/@nsupower?source=rss-bf47b7bb4036------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*aTH1o_5oei5msQsp.</url>
            <title>Stories by rakibul rushel on Medium</title>
            <link>https://medium.com/@nsupower?source=rss-bf47b7bb4036------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 14:08:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nsupower/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[Introduction to OOP — Object, Constructor, Default Constructor]]></title>
            <link>https://medium.com/@nsupower/introduction-to-oop-object-constructor-default-constructor-4eaef530654e?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/4eaef530654e</guid>
            <category><![CDATA[java-object]]></category>
            <category><![CDATA[java-constructor]]></category>
            <category><![CDATA[default-constructor]]></category>
            <category><![CDATA[constructor]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Thu, 23 May 2024 05:57:30 GMT</pubDate>
            <atom:updated>2024-05-23T05:57:30.546Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction to OOP — Object, Constructor, Default Constructor</h3><p>In this chapter, we will learn about objects, constructors, and default constructors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*L06_qqPW22zdmMbz46DGzQ.png" /></figure><p><a href="https://medium.com/@nsupower/introduction-to-oop-pojo-d6ecc5fc17e4"><strong>Previous Chapter</strong></a><br><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><p>In the last chapter, we defined the getter methods for our <strong>Toy</strong> class and our implementation looked like this:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public int getID() {<br>        return ID;<br>    }<br><br>    public void setID(int ID) {<br>        this.ID = ID;<br>    }<br><br>    public int getPrice() {<br>        return price;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public int getQuantity() {<br>        return quantity;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br><br>    <br>}</pre><h4>Object</h4><p>An object is an instance of a class. A class is called a blueprint of an object. We can compare this to real-world objects—for example, the design of a car is the blueprint and when the vehicle comes into existence it becomes an object.</p><p>To create an object of a class we write:</p><pre>Toy car = new Toy();</pre><p>In the above statement, the Toy class is the blueprint and the object is the instance created in the HEAP by the statement <strong>new Toy().</strong></p><p>In the real world, an object (like a human, animal, etc) has properties. For example, a human has hands, eyes, legs, etc. Similarly, in Java an object that exists in the HEAP (real word for Java) has properties. An object of the Toy class has three properties: ID, price, and quantity.</p><h4>Constructor</h4><p>Now let&#39;s create an instance or an object of the class <strong>Toy</strong> and use setter methods to initialize it with the proper value:</p><pre>Toy car = new Toy();<br>car.setID(10);<br>car.setPrice(100);<br>car.setQuantity(3000);</pre><p>The object looks like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*bZeLvmB1A2YZtZv9vZA8Mw.png" /><figcaption>Fig 1.1: object creation</figcaption></figure><p>Now let us try to remove the setter methods from the <strong>Toy </strong>class:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public int getID() {<br>        return ID;<br>    }<br><br>    public int getPrice() {<br>        return price;<br>    }<br><br>    public int getQuantity() {<br>        return quantity;<br>    }<br>    <br>}</pre><p>We cannot write the following anymore, it will give a compile error:</p><pre>Toy car = new Toy();<br>car.setID(10);<br>car.setPrice(100);<br>car.setQuantity(3000);</pre><p>The last 3 statements will give a compile-time error. If we remove those 3 statements then thing looks like the following:</p><pre>Toy car = new Toy();</pre><p>The object created has properties ID, price, and quantity set to 0. Java provides a default value for the properties when an object is created based on the type of the properties. Here, all the properties are of integer type, so Java provides 0 as the default value for all the properties.</p><p>The object looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*BQqNDEnFvcMCvtHWprA3RA.png" /><figcaption>Fig 1.2: Object with default initialization</figcaption></figure><p>Now, how do we create an object that has ID set to 10, price set to 100, and quantity set to 3000? The answer to the question is that we need to use a constructor. So, what is a constructor?</p><p>A<strong> Constructor </strong>is a method with the same name as the class that does not have any return type and is invoked when you create an instance or object from the class. Here is the implementation:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public Toy(int ID, int price, int quantity) {<br>      this.ID = ID;<br>      this.price = price;<br>      this.quantity = quantity;<br>    }<br><br>    public int getID() {<br>        return ID;<br>    }<br><br>    public int getPrice() {<br>        return price;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public int getQuantity() {<br>        return quantity;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br><br>    <br>}</pre><p>Now if we want to create an object from the class we need to write:</p><pre>Toy car = new Toy(10, 100, 3000);</pre><p>If you look at the above statement we used the keyword <strong>new </strong>followed by the constructor<strong> Toy(10, 100, 3000)</strong>. The statement:</p><pre>new Toy(10, 100, 3000)</pre><p>will invoke the constructor (just like methods). We have passed 10, 100, and 3000 as arguments of the constructor. Now, our car instance looks like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*NsfNMh4DO1UVn8lfi2_cFA.png" /><figcaption>Fig 1.3: An object created with a constructor</figcaption></figure><p>So, we can use the constructor to initialize an object. We initialized the car object with an ID equal to 10, price equal to 100, and quantity equal to 3000.</p><p>Now let&#39;s learn about the default constructor.</p><h4>Default Constructor</h4><p>You have seen previously we wrote the following to create an instance:</p><pre>Toy car = new Toy();</pre><p>But there is no constructor defined in the Toy class which has no parameters. Here is our Toy class which does not contain any constructor.</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public int getID() {<br>        return ID;<br>    }<br><br>    public void setID(int ID) {<br>        this.ID = ID;<br>    }<br><br>    public int getPrice() {<br>        return price;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public int getQuantity() {<br>        return quantity;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br><br>    <br>}</pre><p>So, where did the constructor without any parameters come from? the answer is the parameterless constructor is provided by the Java compiler.</p><p>The parameterless constructor is called the <strong>default constructor</strong>. You can also provide the default constructor:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public Toy() {<br>    }<br><br>    public int getID() {<br>        return ID;<br>    }<br><br>    public void setID(int ID) {<br>        this.ID = ID;<br>    }<br><br>    public int getPrice() {<br>        return price;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public int getQuantity() {<br>        return quantity;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br><br>    <br>}</pre><p>Whenever you provide your constructor the compiler will not provide the default constructor. Here we are providing our own constructor:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public Toy(int ID, int price, int quantity) {<br>      this.ID = ID;<br>      this.price = price;<br>      this.quantity = quantity;<br>    }<br><br>    public int getID() {<br>        return ID;<br>    }<br><br>    public int getPrice() {<br>        return price;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public int getQuantity() {<br>        return quantity;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br><br>    <br>}</pre><p>If you now try to do the following:</p><pre>Toy car = new Toy();</pre><p>The compiler will throw a compile-time error because you provide a constructor. The compiler cannot find a constructor without parameters.</p><p>To solve the problem we can also include a default constructor:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public Toy() {<br>    }<br><br>    public Toy(int ID, int price, int quantity) {<br>      this.ID = ID;<br>      this.price = price;<br>      this.quantity = quantity;<br>    }<br><br>    public int getID() {<br>        return ID;<br>    }<br><br>    public int getPrice() {<br>        return price;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public int getQuantity() {<br>        return quantity;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br><br>    <br>}</pre><p>Now the following code will work:</p><pre>Toy car = new Toy();</pre><p>But, why do we need a default constructor? Suppose every toy in our toy store has the price set to 100 dollars initially. All of the other properties will be initialized with 0. Here is the implementation:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public Toy() {<br>      ID = 0;<br>      price = 100;<br>      quantity = 0;<br>    }<br><br>    public Toy(int ID, int price, int quantity) {<br>      this.ID = ID;<br>      this.price = price;<br>      this.quantity = quantity;<br>    }<br><br>    public int getID() {<br>        return ID;<br>    }<br><br>    public int getPrice() {<br>        return price;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public int getQuantity() {<br>        return quantity;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br>}</pre><p>Now let&#39;s create an object of class Toy using the default constructor:</p><pre>Toy car = new Toy();</pre><p>The object will look like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*frlF5_QeJflGPgLK-7Htag.png" /><figcaption>Fig 1.1: Initialization with the default constructor</figcaption></figure><p>Later we can change the properties of the object using the setter methods.</p><p>So, the default constructor can be used to initialize the properties of an object with the values we want.</p><p>That is it for this chapter. The next chapter will be coming soon.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4eaef530654e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to OOP: POJO]]></title>
            <link>https://medium.com/@nsupower/introduction-to-oop-pojo-d6ecc5fc17e4?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6ecc5fc17e4</guid>
            <category><![CDATA[camel-case]]></category>
            <category><![CDATA[pascal-case]]></category>
            <category><![CDATA[getters-and-setters]]></category>
            <category><![CDATA[pojo]]></category>
            <category><![CDATA[setter-method]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Mon, 13 May 2024 06:38:00 GMT</pubDate>
            <atom:updated>2024-05-23T08:23:12.797Z</atom:updated>
            <content:encoded><![CDATA[<p>This chapter will teach us about scope, setter method, getter method, and, POJO</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*L06_qqPW22zdmMbz46DGzQ.png" /></figure><p><a href="https://medium.com/@nsupower/introduction-to-oop-method-execution-1e8d0cbf958a"><strong>Previous Chapter</strong></a><br><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><h4>Introduction to scope.</h4><p>As usual, we will start with our Toy class example.</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public void setID(int id) {<br>        ID = id;<br>    }<br>    <br>    public void setPrice(int p) {<br>        price = price;<br>    }<br><br>    public void setQuantity(int q) {<br>        quantity = q;<br>    }<br>}</pre><p>We will discuss the scope in terms of variables. A scope defines the boundary of a variable where it can be accessed.</p><p><strong>Block scope</strong>: Any variable declared inside a block is only accessible within the block. Remember though it is not possible to access the variable before declaration within the block. This is called block scope.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8738d4b29da93c5a46396043b70b7b0a/href">https://medium.com/media/8738d4b29da93c5a46396043b70b7b0a/href</a></iframe><p>A <strong>block </strong>is defined by the two curly braces (opening and closing). In the above program, a block is started at line 3 and ended in line 7.</p><p>If you compile the above program, the compiler will show you an error saying that it does not find the <strong>id</strong> at line no 9.</p><p><strong>Method scope: </strong>A method scope is similar to a block scope. A method scope starts at the opening curly braces after the method signature and ends at the closing curly braces. The rules for accessing variables are the same as the block scope. The parameters are also considered variables inside the method scope.</p><p><strong>Class scope</strong>: A class scope starts at the opening curly braces after the class name and ends at the closing curly braces.</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public void setID(int id) {<br>        ID = id;<br>    }<br>    <br>    public void setPrice(int p) {<br>        price = price;<br>    }<br><br>    public void setQuantity(int q) {<br>        quantity = q;<br>    }<br>}</pre><p>The variables <strong>ID</strong>, <strong>price</strong>, and <strong>quantity </strong>are variables inside the class. It can be accessed inside any method or any block. The class scope can also be called <strong>global scope</strong>.</p><p><strong>The lifetime of the local variable</strong>: A local variable is a variable declared inside a block or method. The memory for a local variable is allocated by the JVM when the variable declaration statement is executed inside a block or a method. The memory of the local variable is reclaimed by the JVM when the variable goes out of the block or a method.</p><p><strong>The lifetime of the member variable</strong>: The lifetime of the member variable is tightly bound to the lifetime of the instance created from the class. We already know memory required for an instance is taken from the HEAP. The member variables are part of the instance created in the HEAP, so the memory allocated for the member variables is taken from the HEAP. When an instance is no longer required the memory of the instance is reclaimed by the JVM and when that happens the memory for the member variables is reclaimed also.</p><h4>Variable Hiding</h4><p>Variable declared in class scope are hidden by variable declared inside method scope. Let&#39;s see an example of this:</p><pre>public class VariableHiding {<br>    public int value = 10;<br><br>    public void printValue() {<br>        int value = 100;<br>        System.out.println(value);<br>    }<br>}</pre><p>Here we have a local variable named <strong>value </strong>of type <strong>int</strong> inside the method which has the same name as the member variable named <strong>value </strong>of type <strong>int</strong>.</p><pre>public class Main {<br>    public static void main(String[] args) {<br>        VariableHiding variableHiding = new VariableHiding();<br>        variableHiding.printValue();<br>    }<br>}</pre><p>Now, if we run the program, we will see the following output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*BK18Teef9EABjceI3grxoQ.png" /></figure><p>Why is the output showing output? The reason is:</p><p>Here the local variable named value of type <strong>int </strong>hides the member variable named <strong>value</strong> which is also of type <strong>int. W</strong>hen the method printValue() is executed JVM sees the declaration of the local variable named <strong>value</strong> which contains 100 and prints the output.</p><p>Now variable hiding only takes into account the variable name and ignore types. Here is the modified version of the <strong>VariableHiding </strong>class:</p><pre>public class VariableHiding {<br>    public int value = 10;<br><br>    public void printValue() {<br>        char value = &#39;a&#39;;<br>        System.out.println(value);<br>   }<br>}</pre><p>The Main.java remains the same. Now if you run the program, we will get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/1*tpP8O6AaWemTh_FgP8_nYg.png" /></figure><p>You can see that the output shows the character ‘a’.</p><p>Now let&#39;s go back to the same data type example:</p><pre>public class VariableHiding {<br>    public int value = 10;<br><br>    public void printValue() {<br>        int value = 100;<br>        System.out.println(value);<br>    }<br>}</pre><p>Now we want to print 10 instead of local variables value. This can be done using the <strong>this </strong>keyword as <strong>this </strong>acts as a reference variable to the instance created. Here is the code:</p><pre>public class VariableHiding {<br>    public int value = 10;<br><br>    public void printValue() {<br>        int value = 100;<br>        System.out.println(this.value);<br>    }<br>}</pre><p>The output of compiling and running Main.java:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*upphmfueld_w2gNzZca_eA.png" /></figure><p>Now you can see that 10 is printed. So, whenever variable hiding occurs in your code and you want to access an instance&#39;s member variable, use <strong>this</strong> like above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*SkMIVrWBJgWFRV1YHcFIRg.png" /></figure><p>Let’s modify our Toy class to use this:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public void setID(int ID) {<br>        this.ID = ID;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br>}</pre><p>If we take <strong>setID </strong>as an example its parameter name <strong>ID </strong>is the same as the member variable named <strong>ID. </strong>We want to assign the value passed to the parameter ID to the member variable named <strong>ID</strong> we needed to use <strong>this. </strong>If we do not use this then we will end up assigning local variable ID to itself.</p><h4>PascalCase Naming Convention</h4><p>We are using this naming convention in naming the classes so in Java. In this convention, the first character of every word in the name of a class must be in uppercase and other characters must be in lowercase. We have already used this convention. For example, the class name VariableHiding. The name VariableHiding contains two words <strong>variable </strong>and <strong>hiding</strong>. The word variable is made <strong>V</strong>ariable and hiding is made <strong>H</strong>iding. The first characters of both of those words are made bold.</p><h4>CamelCase Naming Convention</h4><p>We are using this convention in naming the variables and methods in Java. This convention is similar to the pascalcase convention except that the first character of the first word must be in lowercase. For example, grossProfit, setID, setPrice, setQuantity, etc.</p><h4>Setter Methods</h4><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public void setID(int ID) {<br>        this.ID = ID;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br>}</pre><p>In the above code set<strong>ID</strong>, set<strong>Price</strong>, and set<strong>Quantity </strong>methods<strong> are </strong>called setter methods. They are called setter methods because they are used to set the value of the instance or member variables of an instance. The naming convention for those methods:</p><ol><li>use the word set</li><li>after that write the member variable name in pascalcase naming convention.</li></ol><p>For example, if we have a member variable named sellingPrice then setter method name must be set<strong>SellingPrice. </strong>The SellingPrice follows pascalcase naming convention.</p><h4>Getter Methods</h4><p>Now let&#39;s see our Toy class definition once again:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public void setID(int ID) {<br>        this.ID = ID;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br>}</pre><p>The Main.java:</p><pre>public class Main {<br>    public static void main(String[] args) {<br>        Toy car = new Toy();<br>        car.setID(10);<br>        car.setPrice(30);<br>        car.setQuantity(10);<br>    }<br>}</pre><p>In the Main.java we are only setting values for the member or instance variables. But, now we want to print those values for the instance car. How should we do it? we cannot directly access it like:</p><pre>System.out.println(car.ID);</pre><p>The answer is we again need a method to get the values. To do that we need to include getter methods.</p><h4>Getter Methods</h4><p>Here is the new implementation after including getter methods:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public int getID() {<br>        return ID;<br>    }<br><br>    public void setID(int ID) {<br>        this.ID = ID;<br>    }<br><br>    public int getPrice() {<br>        return price;<br>    }<br>    <br>    public void setPrice(int price) {<br>        this.price = price;<br>    }<br><br>    public int getQuantity() {<br>        return quantity;<br>    }<br><br>    public void setQuantity(int quantity) {<br>        this.quantity = quantity;<br>    }<br><br>    <br>}</pre><p>The methods getID, getPrice, getQuantity are getter methods. They return the value contained by the instance variables. Let’s analyze getID() getter method:</p><p>The return type of the method is not <strong>void, </strong>it is <strong>int </strong>as we want to return the value of ID and ID has type <strong>int</strong>.</p><pre>public int getID() {<br>    return ID;<br>}</pre><p>The statement inside the body of the method is called the <strong>return</strong> statement. The return statement is used to return value from a method. Here, we return the value of the ID using the statement <strong>return ID;</strong></p><p>Note: When you use the return statement the return type of the method must match the return type of the value. It is better to maintain this rule that&#39;s why I used must.</p><p>Here is the modified Main.java:</p><pre>public class Main {<br>    public static void main(String[] args) {<br>        Toy car = new Toy();<br>        car.setID(10);<br>        car.setPrice(30);<br>        car.setQuantity(10);<br><br>        System.out.println(&quot;The id is: &quot; + car.getID());<br>        System.out.println(&quot;The price is: &quot; + car.getPrice());<br>        System.out.println(&quot;The quantity is: &quot; + car.getQuantity());<br>    }<br>}</pre><p>Here is the output if you compile and run Main.java:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/965/1*W7onBnnSaxIbAUDM8wK96A.png" /></figure><h4>POJO (Plain Old Java Objects)</h4><p>The <strong>Toy </strong>class we have written is a POJO. It only contains member variables, setter methods, and getter methods. There is no special logic attached to it.<br>POJS are the classes that will only have member variables, setter method, and getter method. In member variables must be private, setter methods have only one statement that will set the value of the member variable, and getter methods must have only one statement which is a return statement.</p><p>This is it for this chapter.</p><p><a href="https://medium.com/@nsupower/introduction-to-oop-object-constructor-default-constructor-4eaef530654e"><strong>Next Chapter</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6ecc5fc17e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to OOP — Method Execution]]></title>
            <link>https://medium.com/@nsupower/introduction-to-oop-method-execution-1e8d0cbf958a?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e8d0cbf958a</guid>
            <category><![CDATA[java-method]]></category>
            <category><![CDATA[java8]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Sun, 21 Apr 2024 02:27:07 GMT</pubDate>
            <atom:updated>2024-05-13T06:45:31.486Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction to OOP — Method Execution</h3><p>In this chapter, we are going to have a look at the method execution mainly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*pKZpBlihH93-sKrZM-3xhg.png" /></figure><p><a href="https://medium.com/@nsupower/introduction-to-oop-introduction-to-methods-6bec39137192"><strong>Previous Chapter</strong></a><br><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><p>Before starting this chapter, please make sure that you read the <br><a href="https://medium.com/@nsupower/introduction-to-oop-introduction-to-methods-6bec39137192"><strong>previous chapter</strong></a>.</p><h4>Local Variables, Member Variables and Method Signature</h4><p>Let us start with the example from the previous chapter. Following is Toy.java:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public void setID(int id) {<br>        ID = id;<br>    }<br>    <br>    public void setPrice(int p) {<br>        price = p;<br>    }<br><br>    public void setQuantity(int q) {<br>        quantity = q;<br>    }<br>}</pre><p>Our Main.java file contains:</p><pre>public class Main {<br>  public static void main(String args[]) {<br>    Toy car= new Toy(); <br>    car.setID(10);<br>    car.setPrice(30);<br>    car.setQuantity(50);<br>  }<br>}</pre><p>Remember to put <strong>Main.java</strong> and <strong>Toy.java</strong> in the same folder or directory.</p><p><strong>Local variables</strong>: The variables declared inside the method’s body including the parameters are called <strong>local variables</strong>. For example, the <strong>setID </strong>method has one local variable named <strong>id</strong>. One more local variable is passed by JVM when you invoke the method. We will discuss that soon.</p><p><strong>Member Variables</strong>: Any variables declared outside the method’s body or parameter list are called <strong>member variables</strong>. The <strong>Toy</strong> class has 3 member variables <strong>ID</strong>, <strong>price</strong>, and <strong>quantity</strong>.</p><p><strong>Method Signature</strong>: Method signature in JAVA considers the method name, type, and order of parameters. For example, if you have the following method inside a class:</p><pre>public void print(int width, int height, char c) {<br>  //whatever in the method <br>}</pre><p>The method signature is <strong>print(int, int, char). </strong>The order of the parameter types is maintained. So, <strong>print(char, int, int)</strong> is not a valid method signature of the method as the order is not maintained.</p><h4>Method Execution</h4><p>We start with the previous code example. Here is our <strong>Toy.java</strong>:</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public void setID(int id) {<br>        ID = id;<br>    }<br>    <br>    public void setPrice(int p) {<br>        price = p;<br>    }<br><br>    public void setQuantity(int q) {<br>        quantity = q;<br>    }<br>}</pre><p>Following is the Main.java:</p><pre>public class Main {<br>  public static void main(String args[]) {<br>    Toy car= new Toy(); <br>    car.setID(10);<br>    car.setPrice(30);<br>    car.setQuantity(50);<br>  }<br>}</pre><p>Remember to put Toy.java and Main.java in the same directory or folder.</p><p>Now, when you compile Main.java and run the program and when the statement:</p><pre>car.setID(10)</pre><p>is executed JVM knows on which instance the method is called. It is easy for JVM to understand as you have called the method using the reference variable <strong>car </strong>that points to the instance we have created.</p><p>JVM holds all the local variables of a method along with an extra local variable named <strong>this </strong>when you invoke or call a method. The list of local variables looks like the following:</p><p>List of local variables of setID method: [<strong>this</strong>, id]</p><p>The local variable<strong> </strong>named <strong>this</strong> is a reference variable that points to the same instance as the <strong>car</strong> reference variable<strong>.</strong></p><p>So, now there are two reference variables one is <strong>car </strong>and another is <strong>this </strong>pointing to the same instance.</p><p>You can have multiple reference variables pointing to the same instance. See the following code:</p><pre>Toy car= new Toy();<br>car1 = car;<br>car2 = car1; </pre><p>The reference variable <strong>car </strong>holds the memory location of the instance created in the HEAP. The statement:</p><pre>car1 = car;</pre><p>copies the memory location held by the <strong>car </strong>reference variable<strong> </strong>and assigns it to the <strong>car1</strong> reference variable. Now, car and car1 both reference the same instance. After the execution of the statement <strong>car2 = car1</strong>; three reference variables <strong>car</strong>, <strong>car1</strong>, <strong>car2</strong> references the same instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*cCZvJLQcFBt5GQ9PnktMOw.png" /><figcaption>Fig 1. 1: Multiple references to the same instance</figcaption></figure><p>This is what happened with <strong>car</strong> and <strong>this </strong>reference variable:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*V3nwViaPM3bnx-ZUj8mMNA.png" /><figcaption>Fig 1.2: car and this pointing to same instance</figcaption></figure><p>Now, why does JVM need to include <strong>this </strong>as a local variable? To answer that let’s consider the method setID:</p><pre>public void setID(int id) {<br>  ID = id;<br>}</pre><p>JVM knows that the method setID is part of the class <strong>Toy</strong>. It also knows that <strong>ID</strong> is a member variable. However, it does not know which instance the <strong>ID</strong> member variable belongs to. As you have seen in the <a href="https://medium.com/@nsupower/introduction-to-oop-introduction-to-methods-6bec39137192"><strong>previous chapter</strong></a> we can create multiple instances from the same class. To remedy this problem <strong>this </strong>reference variable<strong> </strong>is used, so that JVM knows which instance the <strong>ID</strong> variable belongs to. We can write the previous code as follows:</p><pre>public void setID(int id) {<br>  this.ID = id;<br>}</pre><p><strong>this.ID</strong> reference to the <strong>ID</strong> member variable of an instance pointed by <strong>this </strong>reference variable. You are not required to write <strong>this.ID, </strong>writing only ID is sufficient as JVM implicitly associates <strong>ID</strong> with the instance referenced by <strong>this </strong>reference variable. But, there are some cases where you will be writing <strong>this </strong>explicitly. In the above code, we write <strong>this </strong>explicitly.</p><p>Now, let&#39;s go back to the code where we called <strong>setID </strong>and passed <strong>10</strong> as the argument:</p><pre>car.setID(10);</pre><p>When the above statement is executed JVM does the following things:</p><p>JVM makes a list of local variables: [<strong>this, id]</strong>. Then argument <strong>10</strong> is assigned to the parameter <strong>id. </strong>Now the variable <strong>id </strong>contains value <strong>10</strong>. When the body of the method executes:</p><pre>ID = id;</pre><p>JVM assigns <strong>10</strong> to the <strong>ID</strong> of the instance referenced by <strong>this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*pbtVb7VAE9Zv3ePj-lys6g.png" /><figcaption>Fig 1.3: Instance after the method setID is executed</figcaption></figure><p>After the statement</p><pre>ID = id;</pre><p>is executed the method <strong>setID</strong> has no more statements inside its body. So, the JVM reclaims all the memory taken by the local variables <strong>this </strong>and <strong>id. </strong>You already know variables take memory if you started the article from the first chapter. After this, JVM returns from the method <strong>setID</strong> and the next statement</p><pre> car.setPrice(30);<br></pre><p>is executed. After the execution of all the statements inside the body of the <strong>setPrice </strong>method JVM return from the method <strong>setPrice </strong>and the next statement:</p><pre>car.setQuantity(50);</pre><p>is executed. When JVM returns from the <strong>setQuantity</strong> method the instance looks like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*4MqrNJ5AWwbzOa8Mbw-Mtg.png" /><figcaption>Fig: 1.4: Instance after setID, setPrice and setQuantity.</figcaption></figure><p>As the above statement is the last one JVM has no more statements to execute so it terminates the program.</p><p>When you create an instance like fig 1.4 all the member variables ID, price, and quantity are called <strong>instance variables</strong>.</p><p>That is it for this chapter.</p><p><a href="https://medium.com/@nsupower/introduction-to-oop-pojo-d6ecc5fc17e4"><strong>Next Chapter</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e8d0cbf958a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to OOP — Introduction to methods]]></title>
            <link>https://medium.com/@nsupower/introduction-to-oop-introduction-to-methods-6bec39137192?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/6bec39137192</guid>
            <category><![CDATA[java-method]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Fri, 29 Mar 2024 15:47:24 GMT</pubDate>
            <atom:updated>2024-04-21T02:32:17.138Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction to OOP — Introduction to methods</h3><p>In this article, we are going to learn about Java methods and some terms and definitions related to methods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*L06_qqPW22zdmMbz46DGzQ.png" /></figure><p><a href="https://medium.com/@nsupower/introduction-to-oop-java-file-basic-rules-access-modifiers-classpath-17bdbdabe5ec"><strong>Previous Chapter</strong></a><br><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><h4>Introduction to Methods</h4><p>We start by answering our last question from the <a href="https://medium.com/@nsupower/introduction-to-oop-java-file-basic-rules-access-modifiers-classpath-17bdbdabe5ec"><strong>previous chapter</strong></a>. The code was:</p><pre>public class Toy {<br>  private int ID;<br>  private int price;<br>  private int quantity;<br>}</pre><p>As we know, we cannot set member variables for the instance car directly with the reference variable like the following anymore:</p><pre>Toy car = new Toy();<br>car.ID = 10;<br>car.price = 30;<br>car.quantity = 1000;</pre><p>It will give you a compile error if you compile <strong>Toy.java</strong>. But there is another way to set the value of the member variables. The way is using <strong>methods</strong>. Rather than talking let&#39;s change the class definition and include <strong>methods</strong>.</p><pre>public class Toy {<br>    private int ID;<br>    private int price;<br>    private int quantity;<br><br>    public void setID(int id) {<br>        ID = id;<br>    }<br>    <br>    public void setPrice(int p) {<br>        price = p;<br>    }<br><br>    public void setQuantity(int q) {<br>        quantity = q;<br>    }<br>}</pre><p>The above code is saved in <strong>Toy.java</strong> and with the help of the following code we can set the values for member variables:</p><pre>Toy car= new Toy(); <br>car.setID(10);<br>car.setPrice(30);<br>car.setQuantity(50);<br> </pre><p>After the execution of the following statement:</p><pre>Toy car= new Toy();</pre><p>The instance looks like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*0pJE78re0OXvVe_ji9JVJQ.png" /><figcaption>Fig 1.1</figcaption></figure><p>After the execution of the following statements:</p><pre>car.setID(10);<br>car.setPrice(30);<br>car.setQuantity(50);<br></pre><p>The instance looks like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*M5uawJcyMt6aK5hOYR0AFg.png" /><figcaption>Fig 1.2</figcaption></figure><p>The following statement:</p><pre>car.setID(10);</pre><p>sets the value of <strong>10</strong> to the <strong>ID</strong> member variable of the instance referenced by the <strong>car</strong> reference variable.</p><p>Similarly, the following statement:</p><pre>car.setPrice(30)</pre><p>sets the value of 30 to the <strong>price </strong>member variable of the instance referenced by the <strong>car</strong> reference variable.</p><p>and the following statement</p><pre>car.setQuantity(50)</pre><p>sets the value of 50 to the <strong>quantity</strong> member variable of the instance referenced by the <strong>car</strong> reference variable. Thus, after executing the above three statements, the instance looks like Fig 1.2.</p><p>We can see that each of the methods performed a particular task. So, every method has a purpose.</p><h4>How to Write A Method</h4><p>Before moving on, I want to show you how to write a method. We will take <strong>setID</strong> method as an example.</p><ol><li>The method will always be inside a class definition. Inside of the class is defined by opening and closing curly braces.</li><li>The access modifier</li><li>The return type.</li><li>The method signature — includes the method name and parameter list. Each parameter in the parameter list is a variable. Note that the parameter list can be empty. <strong>This definition of method signature given here is not exactly the correct one. We will see the real definition in the upcoming chapter.</strong></li><li>Body of the method (A group of statements enclosed by curly braces)</li></ol><p><strong>Step 1</strong>: We already have a class definition.</p><pre>public class Toy {<br>  private int ID;<br>  private int price;<br>  private int quantity;<br>}</pre><p><strong>Step 2</strong>: We will use the <strong>public </strong>access modifier for our method. The code will be following:</p><pre>public class Toy {<br>  private int ID;<br>  private int price;<br>  private int quantity;<br><br>  public<br>}</pre><p><strong>Step 3</strong>: The return type is <strong>void. </strong>It means the method returns nothing. We will have more discussion on return type later. The code becomes:</p><pre>public class Toy {<br>  private int ID;<br>  private int price;<br>  private int quantity;<br><br>  public void<br>}</pre><p><strong>Step 4</strong>: The method name is setID. Now the code becomes:</p><pre>public class Toy {<br>  private int ID;<br>  private int price;<br>  private int quantity;<br><br>  public void setID<br>}</pre><p>The method signature also contains a parameter list. Here we only have one parameter named <strong>id</strong> which has type <strong>int. </strong>The parameter <strong>id </strong>is the variable also. Now the code becomes:</p><pre>public class Toy {<br>  private int ID;<br>  private int price;<br>  private int quantity;<br><br>  public void setID(int id)<br>}</pre><p><strong>Step 5</strong>: The method&#39;s body enclosed by opening and closing curly braces contains only one statement. The code is now:</p><pre>public class Toy {<br>  private int ID;<br>  private int price;<br>  private int quantity;<br><br>  public void setID(int id) {<br>    ID = id;<br>  }<br>}</pre><h4>Method Invocation</h4><p>The following statement is a method invocation:</p><pre>car.setID(10);</pre><p>With the help of method invocation, we can change the properties (member variables) of an instance (object). Here we are changing the ID of the instance referenced by the car variable. We can do a lot more than this with the method but for the time being, let us keep things simple.</p><p>The above statement can be put into words like this: Using a reference variable (which points to an instance of class Toy) named <strong>car </strong>we call the method <strong>setID</strong> on the instance to change the value of the member variable of the instance referenced by the variable <strong>car</strong>.</p><p>Calling a method like this way is called method invocation. When a method is called or invoked the code inside the body of a method starts executing.</p><h4>Arguments</h4><p>You already know about the parameters of the method. But what is an argument? To answer that let&#39;s see an example. Consider the following method invocation:</p><pre>car.setID(10);</pre><p>Here 10 is the value passed to the method when the method is called or invoked. So, 10 is the argument when we call the method. Arguments must match the type and order of the parameters of a method.</p><p>Take as an example the following inside a class named MyPrint:</p><pre>class MyPrint {<br>  public void printValue(int a, char c) {<br>    System.out.println(&quot;First arguments value is &quot; + a);<br>    System.out.println(&quot;Second arguments value is &quot; + c);<br>  }<br>}</pre><p>The printValue method has two parameters the first parameter is <strong>a</strong> and the second parameter is <strong>c</strong>. The variable <strong>a</strong> has type <strong>int</strong> and the variable <strong>c</strong> has type <strong>char</strong>. So, the following code is valid:</p><pre>MyPrint print = new MyPrint();<br>print.printValue(10, &#39;A&#39;);</pre><p>The above method invocation is valid because the arguments 10 and ‘A’ are passed in the correct order maintaining the type of the parameters.</p><p>But the following code is invalid:</p><pre>MyPrint print = new MyPrint();<br>print.printValue(&#39;A&#39;, 10);</pre><p>Here, the arguments do not match the order and type of the parameters. So, you will get a compile error.</p><p><strong>Note</strong>: The method printValue has multiple parameters separated by commas.</p><p>That is it for this chapter. Please wait for the next chapter. I have to write and do my office work. I will do my best to bring the next chapter as soon as possible.</p><p><a href="https://medium.com/@nsupower/introduction-to-oop-method-execution-1e8d0cbf958a"><strong>Next Chapter</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6bec39137192" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to OOP — java file basic rules, access modifiers, classpath.]]></title>
            <link>https://medium.com/@nsupower/introduction-to-oop-java-file-basic-rules-access-modifiers-classpath-17bdbdabe5ec?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/17bdbdabe5ec</guid>
            <category><![CDATA[java-access-modifier]]></category>
            <category><![CDATA[java-classpath]]></category>
            <category><![CDATA[java8]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Tue, 12 Mar 2024 17:38:41 GMT</pubDate>
            <atom:updated>2024-03-29T15:51:28.567Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction to OOP — java file basic rules, access modifiers, classpath.</h3><p>In this chapter, we are going to learn about basic rules to write a java file and learn <strong>access modifiers</strong> and <strong>classpath</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*L06_qqPW22zdmMbz46DGzQ.png" /></figure><p><a href="https://medium.com/@nsupower/oop-introduction-to-class-97ca36b2e498"><strong>Previous Chapter</strong></a><br><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><h4>Basic Rules To Write A Java File</h4><p>There are few rules (at least for now) you need to remember when you write a Java file:</p><ol><li>The file name should be the same as the class name with the extension <strong>.java</strong>.</li><li>The file must contain one public class (This is not necessarily true but as a beginner, it will be easier for you to digest).</li><li>The public class is created by preceding the class name <strong>public</strong> and <strong>class</strong> keywords. Look at the order of how those keywords occurred in the following code. The order is important. If you change the order you will get a compile error.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf663cbfd026d7fc3d5af55d41560c2a/href">https://medium.com/media/bf663cbfd026d7fc3d5af55d41560c2a/href</a></iframe><p>Considering our <strong>Toy</strong> class, first, create a file named <strong>Toy.java</strong> and then write the code in the above figure in that file. If you compile <strong>Toy.java</strong> it will give no error. But try to change the file name to <strong>toy.java </strong>and compile. You will get the following error:</p><p><strong>toy.java:1: error: class Toy is public, should be declared in a file named Toy.java<br>public class Toy {<br> ^<br>1 error</strong></p><p>From the above error, it is clear that if there is a public class the file name (without extension) should be the same as the class name. A java file can contain only one public class. There is a concept of a nested class but for the time being you do not need to worry about it.</p><h4>Introduction to Access Modifiers</h4><p>Now there are some problems with our current <strong>Toy</strong> class definition:</p><pre>public class Toy {<br>  int ID;<br>  int price;<br>  int quantity;<br>}</pre><p>Let us see the first problem:</p><pre>Toy car = new Toy();<br>car.ID = 10;<br>car.price = -10;<br>car.quanity = 10;</pre><p>Now we have created an object of type <strong>Toy</strong> which contains negative price. But, is it possible the price to be negative? the answer is no. So, with current class definition there is no way to prevent it.</p><p>Now another problem is if we change the member variable name ID to toyID like following:</p><pre>public class Toy {<br>  int toyID;<br>  int price;<br>  int quantity;<br>}</pre><p>Now the following code will not work anymore:</p><pre>Toy car = new Toy();<br>car.ID = 10;<br>car.price = 30;<br>car.quanity = 10;</pre><p>You will get compile error because there is no property (member variable) of car which have name ID. To fix this you have to change the code to:</p><pre>Toy car = new Toy();<br>car.toyID = 10;<br>car.price = 30;<br>car.quanity = 10;</pre><p>So changing inside the class is affecting the code we wrote outside of the class.</p><p>To remedy this problems, lets change the class definition by introducing <strong>private </strong>access modifier like following:</p><pre>public class Toy {<br>  private int ID;<br>  private int price;<br>  private int quantity;<br>}</pre><p>With the help of <strong>private </strong>access modifier we prevent direct access of the member variables.</p><p>Here is the implementation of the Main class from where the application starts. Here is the Main.java file:</p><pre>public class Main {<br>  public static void main(String[] args) {<br>     Toy car = new Toy();<br>     car.ID = 10;<br>     car.price = 30;<br>     car.quantity = 10;<br>  }<br>}</pre><p><strong>Note</strong>: Put your <strong>Toy.java</strong> in the same directory as the <strong>Main.java</strong>.</p><p>Now run the command <strong>javac Main.java. </strong>You will get the following error<strong>:</strong></p><p><strong>Main.java:4: error: ID has private access in Toy<br> car.ID = 10;<br> ^<br>Main.java:5: error: price has private access in Toy<br> car.price = 30;<br> ^<br>Main.java:6: error: quantity has private access in Toy<br> car.quantity = 10;<br> ^<br>3 errors</strong></p><p>The errors are basically saying that you cannot directly access member variables using the reference variable anymore as you did so far. For example, <strong>Main.java:6: error: quantity has private access in Toy — </strong>tells<strong> </strong>that you got compile error at line number 6 of the Main.java specifying the error.</p><p>We can remove those errors by changing the access modifier from <strong>private </strong>to <strong>public</strong>:</p><pre>public class Toy {<br>  public int ID;<br>  public int price;<br>  public int quantity;<br>}</pre><p>The <strong>public </strong>access modifier allows you to access the member variable from anywhere with the reference variable. If you now compile the <strong>Main.java</strong> file then the error will be gone.</p><p>When you compile Main.java there will be two class files generated by java compiler in the directory you put in those files. One is <strong>Toy.class</strong> and another is <strong>Main.class</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*guEh9TEBewUQNIa2FhrBvA.png" /></figure><p>In the above figure you can see that Main.java and Toy.java are in folder <br>E:\java8-with-maven\OOP-2. When you compile Main.java java compiler knows you are using Toy.java so it compiles Toy.java also.</p><p>Actually, we can go one step further and delete Toy.java and compile Main.java again. You will see there is no compile error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/1*1gvAzYNNMywfB4pyGJrypg.png" /></figure><p>Look closely we do not have Toy.java in the folder. We have deleted it. Now, let us compile the Main.java again and see if we get any compile error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*_I_ROpQZ_qOW92dRRixedA.png" /></figure><p>So, there is no compile error, java compiler still knows about the Toy class. The reason is <strong>classpath</strong>.</p><h4>Introduction to Classpath</h4><p>The purpose of the <strong>classpath </strong>is to tell java compiler where to look for classes you defined, in particular it looks for files with <strong>.class</strong> extension. The default is the current directory where it looks for class files. So, when you use <strong>Toy </strong>class in <strong>Main.java</strong> it looks for <strong>Toy.class</strong> in the current directory and found the <strong>Toy.class</strong> file and got the definition of <strong>Toy </strong>class from there. We will see more practical usage of <strong>classpath </strong>in more advance discussion.</p><p>Now lets go back to our private example:</p><pre>public class Toy {<br>  private int ID;<br>  private int price;<br>  private int quantity;<br>}</pre><p>So the question now is if we cannot directly access the member variables through reference variable then how the hell we are going to set the value for ID, price and quantity? the answer to that is the <strong>method </strong>which we are going to learn in the next chapter.</p><p><a href="https://medium.com/@nsupower/introduction-to-oop-introduction-to-methods-6bec39137192"><strong>Next Chapter</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17bdbdabe5ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to OOP-Class]]></title>
            <link>https://medium.com/@nsupower/oop-introduction-to-class-97ca36b2e498?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/97ca36b2e498</guid>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[java-class]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Fri, 08 Mar 2024 06:06:12 GMT</pubDate>
            <atom:updated>2024-04-21T11:30:20.039Z</atom:updated>
            <content:encoded><![CDATA[<p>From this chapter, we will start our journey of OOP(Object Oriented Programming) by learning about the class and object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*DHD-ZMGr3ZFLBOclw8hWBw.png" /></figure><p><a href="https://medium.com/@nsupower/control-statements-part-10-c7eceacf8fe2"><strong>Previous Chapter</strong></a><strong><br></strong><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><h4>Introduction to <strong>Class</strong></h4><p>Lets say, we want to develop an application that will represent a toy store. Each toy is identified by its ID. For each toy, we also need to know the price and the quantity.</p><p>Now how do we represent toys in our application? To answer the question first let us understand this:</p><p>We represented integers using the <strong>int </strong>data type<strong>, </strong>characters using the <strong>char </strong>data type, etc<strong>. </strong>like the following;</p><pre>int myInt= 5;<br>char myChar = &#39;A&#39;;<br></pre><p>For the variable <strong>myInt</strong> of type <strong>int,</strong> we have assigned a value of <strong>5</strong> and for the variable <strong>b</strong> of type char, we have assigned a value of <strong>‘A’</strong>.</p><p>As we represented an integer with a <strong>variable</strong> and a <strong>primitive data type</strong>, similarly, we need a <strong>data type</strong> and a <strong>variable</strong> to represent the toy in our code. Now the question is how do we do that?</p><p>There are two things: First, we need to define a <strong>data type</strong> that can represent toy. With the following code, we can do so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf663cbfd026d7fc3d5af55d41560c2a/href">https://medium.com/media/bf663cbfd026d7fc3d5af55d41560c2a/href</a></iframe><p>We use the <strong>public </strong>and<strong> class</strong> keyword to define a data type that will represent a toy in our store.</p><p>Now, the second question is how do we create a variable from the data type <strong>Toy</strong> we have just defined? The answer is similar to the primitive data type. Following is the code:</p><pre>Toy car;</pre><p><strong>car</strong> is a variable of type Toy. The variable created from a class (<strong>Toy</strong> is a class here) is called a <strong>reference variable</strong>. You will see soon why it is named that way. Now, the <strong>car </strong>variable is our reference variable, keep it in mind.</p><p>But, we are missing something, don&#39;t we? A variable needs a value to be assigned otherwise what is the use of the variable? That is why we are going to assign a value to the variable car with the following code:</p><pre>car = new Toy(); <br></pre><p>When a program runs JVM (Java Virtual Machine) allocates a memory area called HEAP from RAM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*HyDij3-7otL_Lj2t_MGQZA.png" /><figcaption><strong>Fig: 1.1</strong></figcaption></figure><p>After the execution of the statement:</p><pre>new Toy();</pre><p>A portion of memory is taken from the HEAP (the large white rectangle) which contains <strong>ID</strong> (initialized with default value 0), <strong>price</strong> (initialized with default value 0), and <strong>quantity</strong> (initialized with default value 0). The large rectangle is the instance of the class <strong>Toy</strong> represented in HEAP at some memory location and this is done by the statement above. So, to create a instance you have to follow the following format:</p><p><strong>new class_name();</strong></p><p><strong>new </strong>is the keyword in java to create an instance.</p><p>Now, how do we get hold of the instance we have just created, that is where we used reference variable:</p><pre>car = new Toy();</pre><p>Now, if you have a look at the Fig 1.1 the <strong>car</strong> variable referencing the instance. So, if you want to work with the instance we have to use the car reference variable.</p><p>Do we want a car toy in our store which costs 0 dollar? of course not. Lets update ID, price and quantity from 0 to some legitimate values. Following is the code:</p><pre>car.ID = 10;<br>car.price = 30;<br>car.quantity = 50;</pre><p>After the execution of the the above three statement the Fig 1.1 becomes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*hRsPEITdULWHCkoMi982ig.png" /><figcaption><strong>Fig: 1.2</strong></figcaption></figure><p>We used the java dot (.) operator to access a <strong>member variable</strong> of an instance. A <strong>member variable</strong> is a variable that is defined inside the class but outside any method or block. As methods and blocks are yet to be discussed, it is enough to know that the <strong>ID</strong>, <strong>price</strong>, and <strong>quantity </strong>are member variables for now. We assign price with the help of the following code using the dot operator:</p><pre>car.price = 30;</pre><p>An instance of a class is also called <strong>object</strong>. The type of instance or <strong>object </strong>is the class you defined.</p><p>You can change the member variables multiple times as you like;</p><pre>car.price = 30;<br>car.price = 500;</pre><p>That is it for this chapter.</p><p><a href="https://medium.com/@nsupower/introduction-to-oop-java-file-basic-rules-access-modifiers-classpath-17bdbdabe5ec"><strong>Next Chapter</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97ca36b2e498" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Control Statements — Part 10]]></title>
            <link>https://medium.com/@nsupower/control-statements-part-10-c7eceacf8fe2?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7eceacf8fe2</guid>
            <category><![CDATA[break-statement]]></category>
            <category><![CDATA[java-8-feature]]></category>
            <category><![CDATA[control-statements]]></category>
            <category><![CDATA[nested-for-loops]]></category>
            <category><![CDATA[continue-statement]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Fri, 12 Jan 2024 13:55:31 GMT</pubDate>
            <atom:updated>2024-01-12T14:00:11.545Z</atom:updated>
            <content:encoded><![CDATA[<h3>Control Statements — Part 10</h3><p>In this chapter, we are going to discuss nested <strong>for</strong> loop with the <strong>break</strong> statement. Then we are going to finish this chapter with the <strong>continue</strong> statement. This chapter is going to be long so please be patient while reading this one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*DHD-ZMGr3ZFLBOclw8hWBw.png" /></figure><p><a href="https://medium.com/@nsupower/control-statements-part-9-a4dc85d48387"><strong>Previous Chapter</strong></a><br><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><h4>Nested For Loop</h4><p>Nested <strong>for</strong> loop is a concept where a for loop is inside another for loop. We want to write a program that will give us the following output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*cwZtNK4MC-_OMESmSs3w0g.png" /><figcaption>Fig: 1.1</figcaption></figure><p>As you can see from Fig 1.1 there are 9 rows we want to print and for each row, we want to print from 1 to 9 in a single line.</p><p>Let us first write a program that will produce the following output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*VJco0qPfLD2AOUDWTuh40w.png" /><figcaption>Fig 1.2</figcaption></figure><p>If you ignore the blank lines and only think about the lines that do not contain a blank line then we can see that all the non-blank lines follow a format:</p><p>“row <strong><em>row_number</em></strong>: <strong><em>”</em></strong></p><p>So, what we need to do is replace only the <strong><em>row_number </em></strong>9 times for each integer 1 to 9 (inclusive)<strong><em>. </em></strong>As we have to repeat a task <br>(printing “row <strong><em>row_number</em></strong>:”) 9 times we can use loops (<strong>do-while</strong>, <strong>while-do</strong>, <strong>for</strong>). Here, we are going to use the <strong>for</strong> loop as the topic is nested <strong>for</strong> loop. Following is the program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/766f45a223b9934cbee5b25d9301a21c/href">https://medium.com/media/766f45a223b9934cbee5b25d9301a21c/href</a></iframe><p>The line number 4 contains the statement for printing row numbers. Until <strong>OOP</strong> is not covered we cannot explain statements involving <strong>System.out.print</strong> or <strong>System.out.println</strong>.</p><p>Now, for each row, we have to print 1 to 9 (inclusive). First let’s see the code which will print 1 to 9 (inclusive):</p><pre>for(int column = 1; column &lt;= 9; column++) {<br>  System.out.print(column + &quot; &quot;);<br>}</pre><p>The above code will print 1 to 9 (inclusive). Now as we have to do this for each row we need to put this code inside the body of the <strong>for</strong> loop of Fig 1.3. The program becomes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9af9d602c932af8c816d9d9003a507b/href">https://medium.com/media/f9af9d602c932af8c816d9d9003a507b/href</a></iframe><p>The <strong>for</strong> loop at line number 3 is called the outer loop and the <strong>for</strong> loop at line number 5 is called the inner loop. The inner loop is inside the body of the outer loop. For each iteration of the outer loop, the inner loop executes. This is where we end our discussion on nested for loop.</p><h4>Break Statement With Nested For Loop</h4><p>Now suppose without changing line number 5 of the program in Fig 1.4 we want to print 1 to 5 (inclusive) for each row. If we read the <a href="https://medium.com/@nsupower/control-statements-part-9-a4dc85d48387"><strong>previous chapter</strong></a> we already know what we need to do right? we are going to use a <strong>break</strong> statement. So let’s modify the program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/881b27e00cc4907917f942d05487a45a/href">https://medium.com/media/881b27e00cc4907917f942d05487a45a/href</a></iframe><p>We get out of the loop using the <strong>break</strong> statement when the variable <strong>counter</strong> is greater than or equal to 6.</p><p>The <strong>break</strong> statement is inside the body of the outer for loop and inner for loop. <strong>The break statement only works on the for loop which is the nearest and the break statement is inside that nearest for loop</strong>. In this case, it is the inner for loop.</p><h4>Continue Statement</h4><p>Now we want to print 5 rows. Without changing the line number 5 of the program in Fig 1.5 we can do that. Let’s see how:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2f7c05669a41817a445dcd1b643b58d/href">https://medium.com/media/b2f7c05669a41817a445dcd1b643b58d/href</a></iframe><p>We added an <strong>if</strong> statement at the beginning of the outer for loop. We wrote:</p><pre>if (row &gt;= 6) {<br>  continue;<br>}</pre><p>What the above code inside the outer loop does is when the variable <strong>row </strong>becomes greater than or equal to 6 the <strong>continue </strong>statement executes. The <strong>continue</strong> statement allows the iteration of the outer for loop to continue but does not allow other statements that follow the <strong>continue</strong> statement whether those statements are inside the if statement at line 6 or after the <strong>if </strong>statement at line 6.</p><p>This is it for this chapter. In the next chapter, we are going to start OOP.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7eceacf8fe2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Control Statements — Part 9]]></title>
            <link>https://medium.com/@nsupower/control-statements-part-9-a4dc85d48387?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4dc85d48387</guid>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[control-statements]]></category>
            <category><![CDATA[break-statement]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Sat, 06 Jan 2024 17:54:03 GMT</pubDate>
            <atom:updated>2024-01-12T14:10:13.502Z</atom:updated>
            <content:encoded><![CDATA[<h3>Control Statements — Part 9</h3><p>In this chapter, we will discuss the <strong>break</strong> statement which is a branch statement.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*DHD-ZMGr3ZFLBOclw8hWBw.png" /></figure><p><a href="https://medium.com/@nsupower/control-statements-part-8-cc5413b96a32"><strong>Previous Chapter</strong></a><br><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><p>The branch statements like other control statements allow you to control the execution of the statements. You will get to understand them after writing some example programs.</p><p>The following branch statements are frequently used in projects:</p><ol><li><strong>break</strong></li><li><strong>continue</strong></li><li><strong>return</strong></li></ol><p>We will show you the most common usage of these branch statements.</p><p>We will start with the <strong>break</strong> statement in this chapter. The <strong>continue statement </strong>will be discussed in the next chapter<strong> </strong>and the<strong> return </strong>statement<strong> </strong>will be discussed in a later chapter as part of the <strong>OOP</strong>.</p><p>The <strong>break</strong> and <strong>continue</strong> branch statements work with <strong>for</strong>, <strong>do-while</strong>, <br><strong>while-do, </strong>and f<strong>or-each</strong> statement. The <strong>switch</strong> statement which is yet to be discussed uses the <strong>break</strong> statement as part of it. The <strong>switch</strong> statement will be discussed after understanding the <strong>enum </strong>type.</p><h4>Branch Statement — break</h4><p>Well, we are going to first write a program with a <strong>do-while</strong> statement. The program asks the user to guess a score which is stored in the program. If the user correctly guesses the score then a congrats message is shown and the program terminates. Otherwise, the program continues to ask you to guess the score until the correct score is guessed. Following is the program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71c524612c2346669ddfc766499ad312/href">https://medium.com/media/71c524612c2346669ddfc766499ad312/href</a></iframe><p>I am not going to go on explaining the above program. If you read the previous articles it should be pretty easy to understand. Particularly, you can read <a href="https://medium.com/@nsupower/control-statement-part-7-b6ee1b675613"><strong>this chapter</strong></a><strong> </strong>to understand the <strong>do-while</strong> statement<strong>.</strong></p><p>Now, let us write the same program using the <strong>break</strong> statement. Following is the program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc9b5cc7c0d5f1d2190ff08b90bcd705/href">https://medium.com/media/cc9b5cc7c0d5f1d2190ff08b90bcd705/href</a></iframe><p>Whenever the userScore is equal to the storedScore the <strong>break</strong> statement is executed. When the <strong>break</strong> statement is executed the execution gets out of the <strong>do-while</strong> statement and starts executing from the very next statement which follows the <strong>do-while</strong> statement. In this case, the statement in line 24 is executed.</p><p>Here is the output of the program in Fig 1.2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P22wmGvXoYtuzyAz2LNiCQ.png" /></figure><p>So, what the <strong>break</strong> statement is essentially doing is getting out of the loop. The <strong>while-do</strong>, <strong>do-while</strong>, <strong>for</strong>, <strong>for-each</strong> all of them considered a loop. As they can execute a bunch of statements (sometimes called the body of a block statement) zero or more times. The bunch of statements in the above program are:</p><pre>System.out.print(&quot;Guess the score: &quot;);<br>            <br>userScore = scanner.nextInt();<br><br>if(userScore != storedScore) {<br>    System.out.println(&quot;Your guess is wrong.....&quot;);<br>}<br>else {<br>    break;<br>}</pre><p>So, using a break statement you can jump out of the loop in which it resides.</p><p>In the next chapter, we will explain the <strong>continue</strong> statement, and nested <strong>for</strong> statements, and discover a fact about the <strong>break</strong> statement when used in a nested <strong>for</strong> statements or loop. The next chapter coming soon.</p><p><a href="https://medium.com/@nsupower/control-statements-part-10-c7eceacf8fe2"><strong>Next Chapter</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4dc85d48387" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Control Statements — Part 8]]></title>
            <link>https://medium.com/@nsupower/control-statements-part-8-cc5413b96a32?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc5413b96a32</guid>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[while-loop]]></category>
            <category><![CDATA[for-loop]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Mon, 01 Jan 2024 15:39:09 GMT</pubDate>
            <atom:updated>2024-01-07T19:52:43.954Z</atom:updated>
            <content:encoded><![CDATA[<h3>Control Statements — Part 8</h3><p>In this chapter, we are going to discuss for statement.</p><p><a href="https://medium.com/@nsupower/control-statement-part-7-b6ee1b675613"><strong>Previous Chapter</strong></a><br><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><h4>For-Loop Statement</h4><p>Please read the <a href="https://medium.com/@nsupower/control-statement-part-7-b6ee1b675613"><strong>previous chapter</strong></a> before reading this one. This is similar to the <strong>do-while</strong> statement. Let&#39;s see the <strong>do-while </strong>statement again and convert it to a <strong>for-loop</strong> statement. First the <strong>do-while</strong> example again:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2282d036ea963a512aab5dbcf1b6e8d/href">https://medium.com/media/e2282d036ea963a512aab5dbcf1b6e8d/href</a></iframe><p>Now let us convert the above program using the <strong>for-loop</strong> statement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da09d8c7712c509d560f9ceaa82ea1bb/href">https://medium.com/media/da09d8c7712c509d560f9ceaa82ea1bb/href</a></iframe><p>This is the simplest form of the for loop:</p><p>Following is the comparison between the <strong>while-do</strong> and <strong>for statement;</strong></p><p>For loop:</p><pre>for(;;) {<br>  System.out.println(counter);<br>}</pre><p>If we consider the <strong>while-do</strong> example the statement inside the red box is executed once. In the case of the <strong>for </strong>statement, this is the same. The statement inside the red box is executed once. Let us modify the for loop:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hf1Tu5vbYwnUjiZCBhVtXQ.png" /></figure><pre>for(int counter = 1;;) {<br>  System.out.println(counter);<br>}</pre><p>Now the expression inside the while-do statement in the green box is evaluated, the same goes for the for statement. If the expression is evaluated to true the body of the for loop which contains a single statement “System.out.println(counter);” is executed. Otherwise, the next statement after the for statement executes. The for loop now:</p><pre>for(int counter = 1; counter &lt;= 3;) {<br>  System.out.println(counter);<br>}</pre><p>After the execution of the body in the while-do statement we incremented the variable <strong>counter. </strong>The statement “counter++” is in the blue box. The same increment is included in the for loop also marked by the blue box. So, now the <strong>for</strong> statement looks like this:</p><pre>for(int counter = 1; counter &lt;= 3; counter++) {<br>  System.out.println(counter);<br>}</pre><p>If you read and understood the <a href="https://medium.com/@nsupower/control-statement-part-7-b6ee1b675613"><strong>previous chapter</strong></a> there is nothing else to say here.</p><p>The <strong>for</strong> statement steps (These are the steps you will find yourself writing mostly):</p><ol><li>Initialization statement (executed only once).</li><li>Evaluate the expression</li><li>if the expression is evaluated to be true then statements in the body of the for statement are executed and go to step 5.</li><li>Otherwise, the next statement if any following the for statement will be executed.</li><li>Generally, increment or decrement the variable initialized at step 1. Go to step 3.</li></ol><p><strong>Note</strong>: In the for statement after counter++ there is no semicolon.</p><p>There is another statement which is called for-each statement which will be discussed later in some upcoming chapter.</p><p>We discussed the for statement in its simplest form which is used mostly. There are other control statements that we are going to discuss in the next chapter called branching statements.</p><p><a href="https://medium.com/@nsupower/control-statements-part-9-a4dc85d48387"><strong>Next Chapter</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc5413b96a32" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Control Statement Part-7]]></title>
            <link>https://medium.com/@nsupower/control-statement-part-7-b6ee1b675613?source=rss-bf47b7bb4036------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6ee1b675613</guid>
            <category><![CDATA[control-statements]]></category>
            <category><![CDATA[do-while]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[do-while-loop]]></category>
            <dc:creator><![CDATA[rakibul rushel]]></dc:creator>
            <pubDate>Wed, 27 Dec 2023 04:36:45 GMT</pubDate>
            <atom:updated>2024-01-01T15:41:17.908Z</atom:updated>
            <content:encoded><![CDATA[<p>In this chapter, we are going to discuss the do-while statement.</p><p><a href="https://medium.com/@nsupower/control-statement-part-6-0d181517d779"><strong>Previous Chapter</strong></a><br><a href="https://medium.com/@nsupower/programming-with-java-8-and-maven-introduction-4561ee11ed66"><strong>First Chapter</strong></a></p><p>You must read the <a href="https://medium.com/@nsupower/control-statement-part-6-0d181517d779"><strong>previous chapter</strong></a> before reading this one.</p><h4>Do-While Statement</h4><p>The <strong>do-while</strong> statement is similar to the <strong>while-do</strong> statement with a small twist. In the case of a <strong>while-do</strong> statement execution of the statements in the body was not guaranteed. If the expression inside the parenthesis is not evaluated to be <strong>true</strong> at the very first time then the statements in the body will never be executed. But, in the case of a <strong>do-while</strong> statement it is ensured that the statements in the body will be executed at least once. Following is the pseudocode of the <strong>do-while</strong> statement:</p><ol><li>Execute the statements in the body of the <strong>do-while</strong> statement.</li><li>Evaluate the expression.</li><li>If the evaluated value is <strong>true </strong>then go to Step 1.</li><li>otherwise, if the evaluated value is <strong>false</strong> then the next statement if any (after the <strong>do-while</strong> statement) executes.</li></ol><p>Let us write the same program we wrote in the previous chapter using the <strong>do-while</strong> statement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9792660f02c291da41c076353d5b0b24/href">https://medium.com/media/9792660f02c291da41c076353d5b0b24/href</a></iframe><p>Let us see each iteration:</p><p><strong>Iteration 1:</strong></p><ol><li>The variable <strong>counter</strong> contains value <strong>1</strong>. Then the variable <strong>counter</strong> is printed to the console and is incremented by 1 (the body is executed at least once). So, the variable <strong>counter</strong> contains 2 now.</li><li>The expression “<strong>counter &lt;= 3</strong>” is evaluated.</li><li>The evaluated value is <strong>true</strong> we need to go to iteration 2 (the body going to be executed a second time).</li></ol><p><strong>Iteration 2:</strong></p><ol><li>The variable <strong>counter</strong> contains value 2. Then the variable <strong>counter</strong> is printed to the console and is incremented by 1. So, the variable <strong>counter</strong> contains 3 now<strong>.</strong></li><li>The expression “<strong>counter &lt;= 3</strong>” is evaluated.</li><li>As the evaluated value is <strong>true</strong> we need to go to iteration 3 (the body going to be executed a third time).</li></ol><p><strong>Iteration 3:</strong></p><ol><li>The variable <strong>counter</strong> contains value 3. Then the variable <strong>counter</strong> is printed to the console and is incremented by 1. So, the variable <strong>counter</strong> contains 4 now<strong>.</strong></li><li>The expression “<strong>counter &lt;= 3</strong>” is evaluated.</li><li>The evaluated value is <strong>false</strong>.</li><li>The execution is done with <strong>do-while </strong>statement and moves to the next statement if any.</li></ol><p>Here is the output of the program:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IbufWx6VxcDS-MgG4ytHnQ.png" /><figcaption>Fit 1.2</figcaption></figure><p>This is it for this chapter.</p><p><a href="https://medium.com/@nsupower/control-statements-part-8-cc5413b96a32"><strong>Next Chapter</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6ee1b675613" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>