- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why do we use import statement in Java? Where it should be included in the class?
The import statement can be used to import an entire package or sometimes import certain classes and interfaces inside the package. The import statement is written before the class definition and after the package statement(if there is any). Also, the import statement is optional.
A program that demonstrates this in Java is given as follows:
Example
import java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList<String> l = new LinkedList<String>(); l.add("Apple"); l.add("Mango"); l.add("Cherry"); l.add("Orange"); l.add("Pear"); System.out.println("The LinkedList is: " + l); } }
Output
The LinkedList is: [Apple, Mango, Cherry, Orange, Pear]
Now let us understand the above program.
The LinkedList class is available in the java.util package. The import statement is used to import it. Then LinkedList l is created. LinkedList.add() is used to add a elements to the LinkedList. Then the LinkedList is displayed. A code snippet which demonstrates this is as follows:
LinkedList<String> l = new LinkedList<String>(); l.add("Apple"); l.add("Mango"); l.add("Cherry"); l.add("Orange"); l.add("Pear"); System.out.println("The LinkedList is: " + l);
- Related Articles
- Why should we use MySQL CASE Statement?
- Why do we use import * and then ttk in TKinter?
- Why do we use interfaces in Java?
- Why we do not import a package while we use any string function?
- Where and how is import statement used in Java programs?
- Why do we need a copy constructor and when should we use a copy constructor in Java?
- Why do we need a wrapper class in Java?
- Can we define a package after the import statement in Java?
- What is SciPy and why should we use it?
- Why we should use whole string in Java regular expression
- What are native methods in Java and where should we use them?
- Why do we get ClassNotFoundException when the class exists in Java?
- Why should we use element in JavaScript?
- Why we should use set.seed in R?
- Why do we cry? Can it be controlled?

Advertisements