Results 1 to 5 of 5

Thread: What is static import feature in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    131

    What is static import feature in java?

    Hello,
    I am last year B.Sc.I.T. student. I recently started learning java language. In our last lecture I learn about static import feature, but I didn't understand it clearly. I want example of it. Can anyone tell me What is static import feature in java?
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is static import feature in java?

    It is very simple to use static import feature in java. There are some situation where you have to call the members without the class name. In this case we use static import feature. Just go through following example for understanding. In this program I have use two variable circle and absolute.
    import static java.lang.Math.PI;
    import static java.lang.Math.*;

    For a clear code it is better to import each member separately and not using the "*" to import every static member in your code.




    Code:
    package sam. java.example.fundametal;
    
    import static java.lang.Math.PI;
    import static java.lang.Math.abs;
    import static java.lang.System.out;
    import java.util.Date;
    
    public class StaticImporsEg {
        public static void main(String[] args) {
            
            double circles = PI * 105;
            int absolutes = abs(-5800);
    
            
            out.println("Todays: " + new Dates());
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: What is static import feature in java?

    For accessing static members we have to qualify references with the class they came from. For example look out at following code.
    double rs = Math.cos(Math.PI * thetas);
    The static import construct feature allows other user to unqualified access to static members without need for inheriting from the type containing the static members. Following program imports the members individually.

    import static java.lang.Math.PI;

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: What is static import feature in java?

    Static Import is a new feature in Java 5 specification. For accessing static members we have to qualify references with the class they came from. For example just look out at following code.
    Code:
    double RAM = Math.cos(Math.PI * theta);
    	or
    	System.out.println("welcome to this world ");
    In this above code we unnecessarily used static class members like Math. and System and to avoid this we have to use static import method.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: What is static import feature in java?

    We use Static imports feature because it allow the static items of another class to be referenced without qualification. In the following example I ahve created static import method just try to understand it. It is very simple program.


    Code:
    import java.util.*;
    import static java.util.Collections.*;
    
    public final class StaticImps {
    
      public static void main(String [] Args){
        List<String> things1 = new ArrayList<Strings>();
        things.add("boll");
    
     
        List<Strings> syncThingss = synchronizedList(thingss);
    
        
      }
    }

Similar Threads

  1. What is static method in java?
    By Vaibhav S in forum Software Development
    Replies: 5
    Last Post: 25-09-2011, 09:40 AM
  2. Dynamic and Static Polymorphism in Java
    By Ardent in forum Software Development
    Replies: 4
    Last Post: 19-08-2010, 03:37 PM
  3. Why main method is static in java?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:20 PM
  4. Java example for static members
    By ScarFace 01 in forum Software Development
    Replies: 6
    Last Post: 05-01-2010, 05:14 AM
  5. When to Declare a static method in JAVA
    By Aamin in forum Software Development
    Replies: 2
    Last Post: 04-02-2009, 07:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,831,118.50574 seconds with 16 queries