{"id":653,"date":"2012-10-02T15:42:48","date_gmt":"2012-10-02T15:42:48","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=329"},"modified":"2016-08-24T08:49:39","modified_gmt":"2016-08-24T08:49:39","slug":"importing-classes-and-methods","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/importing-classes-and-methods\/","title":{"rendered":"Importing Classes and Methods"},"content":{"rendered":"<p><strong>Sample1.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.apryll.package1;\r\n\/\/import static com.apryll.package2.sample2.Funct3;\r\n\r\npublic class Sample1 \r\n{\r\n\tpublic static void main(String&#x5B;] args) \r\n\t{\r\n\t\tSample1 objSample1 = new Sample1();\t\t\r\n\t\t\/\/sample2 objSample2 = new sample2();\r\n\t\t\r\n\t\tSystem.out.println(com.apryll.package2.sample2.City);\r\n\t\t\r\n\t\tobjSample1.Funct2();\r\n\t\t\/\/objSample2.Funct4();\r\n\t\t\r\n\t\t\/\/Sample1.Funct1();\r\n\t}\r\n\t\r\n\tstatic void Funct1()\r\n\t{\r\n\t  System.out.println(&quot;I am Static Function 1&quot;);\r\n\t}\r\n\t\r\n\tvoid Funct2()\r\n\t{\t\r\n\t  System.out.println(Funct3());\r\n\t  System.out.println(City);\r\n\t}\t\t\r\n}\r\n\r\n<\/pre>\n<p><strong>Sample2.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n package com.apryll.package2;\r\n\r\n  public class sample2 \r\n  {\r\n     public static int City = 555;\r\n     public static String Funct3()  \r\n     {\r\n\treturn &quot;Hi there&quot;;\r\n     }\r\n\t\r\n     public void Funct4()  \r\n     {\r\n\tSystem.out.println(&quot;Hello There&quot;);\r\n     }\r\n}\r\n<\/pre>\n<p><strong>1<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  package com.apryll.package1;\r\n  \r\n  import  com.apryll.package2.sample2.*;\r\n\r\n  public class Sample1 \r\n  {\r\n    public static void main(String&#x5B;] args) \r\n    {\r\n      sample2 objSample2 = new sample2();\t\r\n    }\r\n  }  \r\n<\/pre>\n<p>The Above Code Does not Work when creating a object for sample2 since in the import statements is sample2.* is given.It should be   <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport  com.apryll.package2.sample2;\r\n.\r\n.\r\n.\r\n.\r\n<\/pre>\n<p><strong>2<\/strong><br \/>\nWhile Importing class methods and variables the static will not get imported unless you specify static<br \/>\nimport in the import statement<\/p>\n<p><strong>You can Import the Static Methods and Variables in class as below<\/strong><\/p>\n<p>To Import static methods and Variables<\/p>\n<pre>\r\n  import  static com.apryll.package2.sample2.*;  \r\n<\/pre>\n<p>To Import static methods and Variables<\/p>\n<pre>\r\n  import  static com.apryll.package2.sample2.*;  \r\n<\/pre>\n<p>To Import static Variable City<\/p>\n<pre>\r\n  import  static com.apryll.package2.sample2.City;  \r\n<\/pre>\n<p>To Import static Function getCityName<\/p>\n<pre>\r\n  import  static com.apryll.package2.sample2.getCityName;  \r\n<\/pre>\n<p><strong>3<\/strong><br \/>\nYou can directly access static method and Variable declared in some other package in static void main() method as follows<\/p>\n<pre>\r\n  System.out.println(com.apryll.package2.sample2.City);\r\n  System.out.println(com.apryll.package2.sample2.Funct3());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sample1.java package com.apryll.package1; \/\/import static com.apryll.package2.sample2.Funct3; public class Sample1 { public static void main(String&#x5B;] args) { Sample1 objSample1 = new Sample1(); \/\/sample2 objSample2 = new sample2(); System.out.println(com.apryll.package2.sample2.City); objSample1.Funct2(); \/\/objSample2.Funct4(); \/\/Sample1.Funct1(); } static void Funct1() { System.out.println(&quot;I am Static Function 1&quot;); } void Funct2() { System.out.println(Funct3()); System.out.println(City); } } Sample2.java package com.apryll.package2; public class sample2 {&hellip; <a href=\"https:\/\/codethataint.com\/blog\/importing-classes-and-methods\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-653","post","type-post","status-publish","format-standard","hentry","category-classes-and-objects"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=653"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":1465,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/653\/revisions\/1465"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}