Java Static Import Explained With Easy Example With Source Code

What is Static Import?

Static Import is a feature in Java that allows you to access the methods and variables that are defined as public static to be used in a class without having to use the class name.

Let’s say there is a following class with a static field and a static method.

package com.sample;
public Class MyClassOne() {
	public static int a_number = 3;
	
	public static void doSomething(int number) {
		System.out.println("Did Something " + number);
	}
}


Now let’s create another class that use variable and method from this class. I am not suggesting you should make your variable public – this is for demonstration purpose only. A good programming practice is to declare your variables private and expose them via getters and setters method as necessary.

Regular Way (Non Static Import)

package com.sampletwo;

import com.sample.MyClassOne;

public Class MyClassTwo() {
	
	
	public static void doSomethingTwo() {
		MyClassOne.doSoemthing(MyClassOne.a_number);
	}
}


With Static Import, the code becomes

package com.sampletwo;

import com.sample.MyClassOne.*;

public Class MyClassTwoStatic() {
	
	
	public static void doSomethingTwo() {
		doSomething(a_number);
	}
}


Note: Do not statically import methods with a name that is identical to a method in Object, because methods that are naturally in scope take precedence over static imports

  1. All About Java Garbage Collection : Types, Algorithms, Advantages and Disadvantages
  2. Java Static Import Explained With Easy Example With Source Code
  3. SQL Example To Create Table On MySql DB
  4. Book Review – Drools JBoss Rules 5.X Developer’s Guide by Micahal Bali (Packt Publishing)
  5. List of valid @suppresswarning annotation parameters in Java for Eclipse and IBM RAD IDE
  6. Finding Java System Properties
  7. List of Major Java 7 Features – How java programming language has changed in JDK 7
  8. Date From Timestamp in Long format
  9. How to resolve – java.lang.VerifyError java.lang.SecurityException: Signers of org.junit.runner.Runner do not match signers of other classes in package
  10. Complete List Of Java Keywords
  11. List of 25 Plus Devices That Run On Java
  12. What is the difference between java and javaw?
  13. These 100 plus programming languages run on Java Virtual machine (List)
  14. Read XML File In JAVA using DOM – A Simple Tutorial
Tagged , , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Read XML File In JAVA using DOM – A Simple Tutorial

  1. harry says:

    I need a tutorial on an exact opposite of this excellent article. That is I need to serialize Java Object to XML. Can you help me in that Kushal??? I wud appreciate it a lot.

    Thanks

  2. Jim says:

    Hey Kushal!
    This is very great and helpful, but I got another question about that. If I have an Element which hasn’t any values, I always get a NullpointerException. Do you have an Idea how to fix/catch that?

    Thank you!

  3. Mridula says:

    The advantage in Java is that there it is not necessary to take care of objects which are no longer used. Garbage collector takes care of these objects unlike C/C++ language where because of this issue the program terminates causing OutOfMemoryErrors.

Leave a Reply

Your email address will not be published. Required fields are marked *