tags:

views:

96

answers:

2

Hello Friends!

I want to know about the util Class used in Java. I am currently working on one Application in Android where I need to used a class from one file to another different file. I was told to import it like "import com.android.utli.(ClassName)" here the "com.android.util" is a package name.

Can I use thast class in my another file simply by importing the package along with the Class Name?

Getting slightly confused.

Thanks, david

+2  A: 

The package is really a part of the "fully qualified class name". You can actually use that fully qualified name everywhere you use the class name, e.g.

com.android.util.UtilClass myUtil = new com.android.util.UtilClass();

But that's rather inconvenient. Therefore, classes in the same package (and in the package java.lang) can be used shorthand (i.e. only the class name) without imports. Classes from other packages have to be imported before they can be used that way.

It's just a way to organize the code and prevent problems when different programmers write classes with the same name - as long as they are in different packages, the collision can always be resolved by using the fully qualified class name.

Michael Borgwardt
+2  A: 

Yes. After you import the class at the top of your java file like:

import android.util.Log;

You can then use it in your code.

Log.debug("MyActivity", "Thing I want to log");
Peter DeWeese