{"id":15664,"date":"2025-06-10T10:19:19","date_gmt":"2025-06-10T10:19:19","guid":{"rendered":"https:\/\/gaviki.com\/blog\/?p=15664"},"modified":"2025-06-10T10:19:22","modified_gmt":"2025-06-10T10:19:22","slug":"which-is-true-about-the-static-import-statement-in-java","status":"publish","type":"post","link":"https:\/\/gaviki.com\/blog\/which-is-true-about-the-static-import-statement-in-java\/","title":{"rendered":"Which is true about the static import statement in Java"},"content":{"rendered":"\n<p>Which is true about the static import statement in Java? 1) It is used when one wants to use a class&#8217;s static members. 2) It is the same as the import statement. 3) When the static import statement is used in a Java source file, the classes declared in that file cannot declare any non-static members. 4) The statement can be written either as static import or import static. It does not matter how it is written.<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>The correct answer and explanation is:<\/strong><\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>Correct Answer: 1) It is used when one wants to use a class&#8217;s static members.<\/strong><\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddfe <strong>Explanation (300 words):<\/strong><\/h3>\n\n\n\n<p>In Java, the <strong>static import<\/strong> statement allows you to access static members (fields and methods) of a class <strong>without qualifying them with the class name<\/strong>. This helps make code more concise and readable when you frequently use static members from a utility class like <code>java.lang.Math<\/code>.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import static java.lang.Math.*; \/\/ static import\n\npublic class Example {\n    public static void main(String&#91;] args) {\n        double result = sqrt(16); \/\/ no need to write Math.sqrt(16)\n        System.out.println(PI);   \/\/ no need to write Math.PI\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Without static import, you&#8217;d have to use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double result = Math.sqrt(16);\nSystem.out.println(Math.PI);\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Let&#8217;s break down the options:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u2705 <strong>It is used when one wants to use a class&#8217;s static members.<\/strong><br>\u2705 This is correct. That\u2019s exactly what static import is for: to use static methods or variables without class qualification.<\/li>\n\n\n\n<li>\u274c <strong>It is the same as the import statement.<\/strong><br>This is false. The regular <code>import<\/code> statement is for importing <strong>types (classes or interfaces)<\/strong>, while <code>static import<\/code> is specifically for <strong>static members<\/strong>.<\/li>\n\n\n\n<li>\u274c <strong>When the static import statement is used in a Java source file, the classes declared in that file cannot declare any non-static members.<\/strong><br>This is incorrect. Static import has <strong>no effect<\/strong> on whether a class can declare static or non-static members. It&#8217;s just a convenience for referencing static members.<\/li>\n\n\n\n<li>\u274c <strong>The statement can be written either as static import or import static. It does not matter how it is written.<\/strong><br>This is false. The correct syntax is <code>import static<\/code>. Reversing the order will result in a <strong>compiler error<\/strong>.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>Conclusion:<\/strong><\/h3>\n\n\n\n<p>The <strong>correct statement<\/strong> is <strong>Option 1<\/strong>:<br><strong>Static import is used when one wants to use a class&#8217;s static members<\/strong> without class name qualification.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Which is true about the static import statement in Java? 1) It is used when one wants to use a class&#8217;s static members. 2) It is the same as the import statement. 3) When the static import statement is used in a Java source file, the classes declared in that file cannot declare any non-static [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-15664","post","type-post","status-publish","format-standard","hentry","category-quiz-questions"],"_links":{"self":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/15664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/comments?post=15664"}],"version-history":[{"count":1,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/15664\/revisions"}],"predecessor-version":[{"id":15665,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/15664\/revisions\/15665"}],"wp:attachment":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/media?parent=15664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/categories?post=15664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/tags?post=15664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}