Using Import Statements

Using Import Statements

We can use import function in the two ways :
  1. wildcard import
  2. explicit class import

Import statements are little more than a way for us to save keystrokes when we\'re typing our code. We essentially give the class a longer name, which we call the fully qualified name.

The compiler would never know what you meant if you typed the following:

class Bar {
void doStuff() {
Foo f = new Foo(); // Here you want the WickedlySmart version
} // But how will the compiler know?
}

To eliminate the confusion, you\'re required to do one of two things to help the
compiler:

1. Use an import statement,

import com.wickedlysmart.Foo;
class Bar {
void doStuff() {
Foo f = new Foo(); // Now the compiler knows which one to use
}
}

or
2. Use the fully qualified name throughout your code:

class Bar {
void doStuff() {
com.wickedlysmart.Foo f = new com.wickedlysmart.Foo() // No doubts
}
}

By:UMANG
Date:2025-02-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher