{"id":202,"date":"2016-05-19T08:18:00","date_gmt":"2016-05-19T08:18:00","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=202"},"modified":"2021-01-12T17:13:29","modified_gmt":"2021-01-12T11:43:29","slug":"java-static-import-example","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-static-import-example\/","title":{"rendered":"Java static import example"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><ul><li><a href=\"#Without_using_static_import\">Without using static import :<\/a><\/li><\/ul><\/li><li><a href=\"#With_using_static_import\">With using static import :<\/a><ul><li><a href=\"#Advantages\">Advantages:<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<div dir=\"ltr\" style=\"text-align: left;\">\n<div dir=\"ltr\">\n<p>If any class which is not in same package, we need to import it. If we import that class we can directly access static variables and methods with the class name.<\/p>\n<\/div>\n<div dir=\"ltr\">If you use static import,\u00a0 you do not need to use class name any more.<\/div>\n<div dir=\"ltr\"><b><br \/>\n<\/b><b>Lets understand with the help of example<\/b><\/div>\n<div dir=\"ltr\">\n<h4 style=\"text-align: left;\"><span id=\"Without_using_static_import\">Without using static import :<\/span><\/h4>\n<pre name=\"code\">package org.arpit.java2blog;\n\npublic class StaticImportMain {\n\n    public static void main(String[] args) {\n        System.out.println(\"With Static import\");\n        System.out.println(\"Value of PI : \"+Math.PI);\n    }\n\n}<\/pre>\n<\/div>\n<div dir=\"ltr\">\n<h3 style=\"text-align: left;\"><span id=\"With_using_static_import\">With using static import :<\/span><\/h3>\n<\/div>\n<pre name=\"code\">package org.arpit.java2blog;\nimport static java.lang.System.out;\nimport static java.lang.Math.PI;\n\npublic class StaticImportMain {\n\n    public static void main(String[] args) {\n        out.println(\"With Static import\");\n        out.println(\"Value of PI : \"+PI);\n    }\n\n}<\/pre>\n<p>As you can see, we have directly used out.println and PI without using class name because \u00a0we have used static import here.<\/p>\n<h4 style=\"text-align: left;\"><span id=\"Advantages\"><b>Advantages:<\/b><\/span><\/h4>\n<div>If you have lot of static variables, you have to write less code.<\/div>\n<div><b>\u00a0<\/b><\/div>\n<div><b>Disadvantages:<\/b><\/div>\n<div><b>\u00a0<\/b><\/div>\n<div>It is very hard to read and\u00a0unmaintainable.<\/div>\n<p><b><br \/>\n<\/b><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsWithout using static import :With using static import :Advantages: If any class which is not in same package, we need to import it. If we import that class we can directly access static variables and methods with the class name. If you use static import,\u00a0 you do not need to use class name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[180],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/202"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}