Packages

Java Syntax
Level 5 , Lesson 2
Available

"Hi, Amigo! Today we'll talk about packages."

"Files on a computer are grouped into folders. Classes in Java (every class is stored in a separate file) are grouped into packages, which correspond to folders on the hard drive. So, there's nothing new here. There are two things I'd like to point out, though."

"First, a class's full unique name consists of its package name plus the class name. Here are some examples:"

Full unique name Package name Class name
java.io.FileInputStream java.io FileInputStream
java.lang.String java.lang String
java.util.ArrayList java.util ArrayList
org.apache.tomcat.Servlet org.apache.tomcat Servlet
Cat Not specified Cat

"A full class name is always unique."

"It would be a pain if we had to write the long name, i.e. java.util.ArrayList, every time. That's why Java lets you import classes. You can use other classes' short names in your code, but at the beginning of your class you must explicitly indicate which classes you will use."

"How do you do that?"

"With a line that looks like this: import java.util.ArrayList;"

"At the beginning of a class, immediately after declaring the package, you can indicate which class you are referring to when you use ArrayList in your code."

"Why overcomplicate things? Can classes have identical names?"

"Yes. There can be classes with the same name in different packages. We can't import two classes with identical names, so we would have to call one of them by its full name."

"Here is an analogy for you. You have a colleague named Jim. No problem with that: everybody knows who he is. But if there were three Jims in your office, you'd have to call them by their full unique names to avoid confusion."

"Second, it's always better to place classes into packages, not the root src folder. When you don't have many classes, this isn't a problem, but when there are many, it's easy to mix them up. Always create classes inside packages."

In Java, the common practice is to give classes and packages meaningful names. Many companies release their libraries (sets of classes) and name them after their company or website to avoid confusion:"

Package name Company/project name
org.apache.common
org.apache.tomcat
org.apache.util
Apache
com.oracle.jdbc Oracle
java.io
javax.servlet
Sun, Java
com.ibm.websphere IBM, WebSphere
com.jboss JBoss
Comments (19)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Cody Level 23, San Francisco, United States
24 December 2020
Ok.
Mihai Bone Level 8, Bucharest, Romania
6 October 2020
In the company I work there are a lot of programmers and I can share with you a pattern. 1 Line of code 4 Breaks 6 Coffees 4 Hour meeting (What is nice about codegym is they motivate you also to develop independently)
queldorm Level 11, East coast, United States
16 October 2020
meeting recap: switch coffee brand and 1 programmer fired.
Sinisa Level 11, Banja Luka, Bosnia and Herzegovina
19 February 2021
😄
Ice_ Beam Level 7, Germany
4 May 2021
Heh I bet they get paid a lot for doing 1 line of code
8 January 2020
I'm afraid of what comes next :D
Maryem Vickers Level 7, HT..., United Kingdom
17 August 2020
laugh out loud!!!!!!!!!!
Austeja Level 10, Kaunas, Lithuania
2 January 2020
I truly love this course.
Alexandre Lalancette Level 41, Quebec, Canada
23 October 2019
Love it!
Cam Level 22, Austin, United States
14 October 2019
Okay!
Maryem Vickers Level 7, HT..., United Kingdom
17 August 2020
XD How many okays in a row are there?!?!?!?!?!
Emile Level 14, Paris, France
16 September 2019
Very good!!
Gabriella LaPlace Level 16, Basseterre, Saint Kitts and Nevis
30 July 2019
Okay.
Renat Mukhametshin Level 16, Pervouralsk, Russain Federation
1 April 2019
oh, very good
Tayyab Mubeen Level 16, Lahore, Pakistan
23 September 2018
lovely..!!!!!
Ashutosh Sharma Level 8, Delhi, India
14 March 2019
Wow!! Lahore. Is Lahore beautiful??
Tayyab Mubeen Level 16, Lahore, Pakistan
24 March 2019
yes...so much..!!!
Ashutosh Sharma Level 8, Delhi, India
24 March 2019
That's great. Delhi is beautiful too
Abdur Rehman Level 7, Faisalabad, Pakistan
13 May 2019
lahore is really beautiful