Total members 11895 |It is currently Mon Dec 30, 2024 5:30 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Static Import usage in java :
Suppose you have a static member of a class included in an other package, instead of import the class you just can import the static member (Feature introduced in J2SE5) .
Hints about using static import:
  1. Use static keyword in after the import keyword .
  2. You can import static variables ,object references and methods .
  3. You can use the (.* ) to import all the static members .
  4. Members imported using static could be used without references or class names.
  5. Static import usually used to load constants to your class.
  6. Too many static imports in your class is harmful for code readability and maintenance in the future.

Without using static import


Using of variable limt which is a static member of HasStaticMember class.
java code
package CurveOr;
import CurveIT.HasStaticMember;

public class UsingStaticImport {
public static void main(String[] args) {
System.out.println(HasStaticMember.limt);
}
}

The class HasStaticMember is on another package "CurveIT":
java code
package CurveIT;
public class HasStaticMember {
public static int limt=100;
}


Keep in mind that the HasStaticMember class is not at the same package of the importing class.


Using static import with static variables


java code
package CurveOr;
import static CurveIT.HasStaticMember.limt;

public class UsingStaticImport {
public static void main(String[] args) {
System.out.println(limt);
}
}
}


As you can notice ,
java code
import static CurveIT.HasStaticMember.limt;

The upper statement is used to import static members .

Using static import with static functions


The class HasStaticFunction has a static function println() which we are going to apply a static import on it at UsingStaticImport class:
java code
package CurveIT; 
public class HasStaticFunction {
public static void println()
{
System.out.println("I am HasStaticFunction");
}

}

The usage :
java code
package CurveOr;
import static CurveIT.HasStaticFunction.println;

public class UsingStaticImport {
public static void main(String[] args) {
println();
}
}

As you see the function println it statically imported :
java code
import static CurveIT.HasStaticFunction.println;

and used directly using its name without any references or class name.

Importing static variables and functions at the same time :


You can all the static members in a class using only one static import statement as follows :
java code
package CurveIT;


public class HasStaticMember {
public static int limit=100;
public static void setLimit(int newlimt)
{

limit=newlimt;
}
}

Imported at:
java code
package CurveOr;
import static CurveIT.HasStaticMember.*;

public class UsingStaticImport {
public static void main(String[] args) {
setLimit(43);
System.out.println("Limit is"+limit);
}
}


The output of this code is :
Code:
Limit is 43




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Static Import
 difference between a static and a non-static inner class     -  
 import XSD file in another     -  
 Import text file with ASP.NET     -  
 import Mysql database     -  
 How to import your website dump database script     -  
 Import a PowerPoint presentation into Windows Movie Maker     -  
 What is a static method     -  
 static vs fixed div position     -  
 Static Methods and Variables     -  
 Class static properties     -  



Topic Tags

Java Basics
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com