{"id":4061,"date":"2017-09-16T06:41:10","date_gmt":"2017-09-16T06:41:10","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=4061"},"modified":"2021-03-21T06:47:13","modified_gmt":"2021-03-21T06:47:13","slug":"java-inner-class-demo-program","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/java-inner-class-demo-program\/","title":{"rendered":"Java Inner class demo program"},"content":{"rendered":"<p>An inner class is a class inside another class. The main purpose of using an inner class is to group related classes together.<\/p>\n<h3>Java program<\/h3>\n<pre>import java.text.DecimalFormat;\r\n\r\npublic class Item\r\n{\r\n    private String description;  \/\/ description\r\n    private int itemNumber;      \/\/ number\r\n    private Cost cost;       \/\/ Cost\r\n\r\n    \/**\r\n     * Item constructor\r\n     **\/\r\n    public Item(String desc, int itemNum, double wholesale, double retail)\r\n    {\r\n        description = desc;\r\n        itemNumber = itemNum;\r\n        cost = new Cost(wholesale, retail);\r\n    }\r\n\r\n    \/**\r\n     * RetailItem class toString method\r\n     *\/\r\n    public String toString()  {\r\n        String str;\r\n        DecimalFormat dollar = new DecimalFormat(\"#,##0.00\");\r\n        str = \"Description: \" + description\r\n                + \"\\nItem Number: \" + itemNumber\r\n                + \"\\nWholesale Cost: $\"\r\n                + dollar.format(cost.getWholesale())\r\n                + \"\\nRetail Price: $\"\r\n                + dollar.format(cost.getRetail());\r\n        return str;\r\n    }\r\n\r\n    \/**\r\n     * Inner Class\r\n     *\/\r\n    private class Cost\r\n    {\r\n        public double wholesalePrice;\r\n        public double retailsalePrice;\r\n\r\n        public double getWholesale() {\r\n            return wholesalePrice;\r\n        }\r\n\r\n        public void setWholesale(double wholesale) {\r\n            this.wholesalePrice = wholesale;\r\n        }\r\n\r\n        public double getRetail() {\r\n            return retailsalePrice;\r\n        }\r\n\r\n        public void setRetail(double retail) {\r\n            this.retailsalePrice = retail;\r\n        }\r\n\r\n        \/**\r\n         * Cost constructor\r\n         *\/\r\n        public Cost(double w, double r)\r\n        {\r\n            wholesalePrice = w;\r\n            retailsalePrice = r;\r\n        }\r\n    }\r\n}<\/pre>\n<pre><\/pre>\n<h3>Test Driver<\/h3>\n<pre>\/**\r\n * Item class\r\n *\/\r\npublic class InnerClassDemo\r\n{\r\n    public static void main(String[] args)\r\n    {\r\n        Item item = new Item(\"Rice Packet\", 1001,1.75, 2.50);\r\n        System.out.println(item);\r\n    }\r\n}<\/pre>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4069\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inner-class.png\" alt=\"Inner class\" width=\"1735\" height=\"975\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inner-class.png 1735w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inner-class-300x169.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inner-class-1024x575.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inner-class-768x432.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Inner-class-1536x863.png 1536w\" sizes=\"auto, (max-width: 1735px) 100vw, 1735px\" \/><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-4061\" data-postid=\"4061\" class=\"themify_builder_content themify_builder_content-4061 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>An inner class is a class inside another class. The main purpose of using an inner class is to group related classes together. Java program import java.text.DecimalFormat; public class Item { private String description; \/\/ description private int itemNumber; \/\/ number private Cost cost; \/\/ Cost \/** * Item constructor **\/ public Item(String desc, int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-4061","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=4061"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4061\/revisions"}],"predecessor-version":[{"id":19744,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4061\/revisions\/19744"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=4061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=4061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=4061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}