Sample1.java
package com.apryll.package1; //import static com.apryll.package2.sample2.Funct3; public class Sample1 { public static void main(String[] 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("I am Static Function 1"); } void Funct2() { System.out.println(Funct3()); System.out.println(City); } }
Sample2.java
package com.apryll.package2; public class sample2 { public static int City = 555; public static String Funct3() { return "Hi there"; } public void Funct4() { System.out.println("Hello There"); } }
1
package com.apryll.package1; import com.apryll.package2.sample2.*; public class Sample1 { public static void main(String[] args) { sample2 objSample2 = new sample2(); } }
The Above Code Does not Work when creating a object for sample2 since in the import statements is sample2.* is given.It should be
import com.apryll.package2.sample2; . . . .
2
While Importing class methods and variables the static will not get imported unless you specify static
import in the import statement
You can Import the Static Methods and Variables in class as below
To Import static methods and Variables
import static com.apryll.package2.sample2.*;
To Import static methods and Variables
import static com.apryll.package2.sample2.*;
To Import static Variable City
import static com.apryll.package2.sample2.City;
To Import static Function getCityName
import static com.apryll.package2.sample2.getCityName;
3
You can directly access static method and Variable declared in some other package in static void main() method as follows
System.out.println(com.apryll.package2.sample2.City); System.out.println(com.apryll.package2.sample2.Funct3());