Package in Java
Sakshi Agrawal
immediate joiner | Backend Developer | Java |Spring Boot|Microservices|Hibernate|Jenkins|Bitbuket|Kubernetes|kafka|Putty|Cassandra|Software Engineer
Hello to my all Readers!....
Hope You All are having good Reading
Below I'm sharing my Todays learning regarding Technical Concept (Java Language). Have a glance on this and enjoy your reading. Hope you all might get benefited from it!
Package in java
What is package?
What is need of package?
How to create package?
Syntax:-?package<package name>;
???????Ex: package p1;
Where we can declare package?
At the top of the java file or it should be the first statement of java file.
How many types of packages are their and what are they?
==>The package (parent or sub) which is?already build-in predefine java file by the sun and other vendors, like??lang, awt, javax, swing, io, util etc.. Is called pre-define(parent and sub) package.?
==>The package define by us as per the requirement of project like package p1;?package p2; ..are known as user define(parent or sub) package.
?Default:- package statement is optional. If we define a class without package statement, then that class is said to be available in "default package "i.e.; current working directory.
---------//---------
Bullets points:-
Creation process of package in java file:-
2. Compiler does not create package physically in current working directory just with javac command. Package classes must be compiled with "-d" option to create package physically.
Syntax:- javac-d<path in which package should be copied> source filename.
For Example >javac -d . Example.java
With this command compiler creates package "p1" with "Example.class" and places it in current working directory. Operator "." represents current working directory.
>javac -d D:\Program Example.java
3. With this command compiler creates package "p1" with "Example.java" in?????D:\Program folder.
Rule: Program folder must be existed in D drive before this program compilation, else it leads to CE.
--------------------------------------------------------------------------
What is the functionality of "-d"?
Its actual functionality is creating package with the name mentioned in java file and moving all?.class files in that package, and finally storing that package in the given path.
?Creating?another file in same package with different name:
Test class file created in same package on given class path.
----------------------------/------------------------------
How Compiler change the code after providing package ?
After compilation?compiler replace class name and its constructor name with its packagename.classname. It is called fully qualified name
Note:-To execute java file belongs to package we must use package name to execute it -
>java p1.Example
To see compile generated .class we must use-
>javap Example.class
Why we must use package name in execution?
As you observed, in Example.class the class is changed as p1.Example. Since name is p1.Example, it must be executed with the same name means in execution we must use package name.?
Can we execute a class from current working directory that is placed in another directory?
No, it lead to exception "java.lang,NoClassDefFoundError"
Note:-If package is placed in another directory, we must update its path in Classpath environment variable, else it leads above exception.
Updating Classpath environment variable:-
1. By using java command option "-cp"?or??"- classpath".
2. By using "Set Classpath" command
3. By using "Environment Variables window".
----------------------/--------------------
What Classloader working functionality?
When we are using a class from another class, should I compile that class first ?
1.???First it searches for that Example?class definition in sample.java, if not found.
?2.???It searches for Example.class in Sample class package, if not found.
3.???It searches for Example.java in Sample class package, if not found.
4.???Then compiler terminates Sample.java file?compilation by throwing CE: cannot find symbol
?5. if found in same defination sample.java
??compile and generated Sample.class file and also?Example.class file
6. Within Same?package?but package is already created and one of the?class is inside it -
7.???If Example.java is found, it searches for Example class definition in Example.java file. If it is found,???compiler compile?entire java file, it means it also compiles other class definitions?and generates those class's .class files. Else terminates Sample.java file compilation with above compilation error.
?8.???If Example.class is found, it also searches for Example.java. If not found, compiler uses Example.class file directly.
9.???If Example.java is also available, it checks modified time of both files, if Example.java file , modified date is greater than Example.class modified date, it compiles Example.java again for generating Example.class with its latest changed java code.
=>Within in same package but classes are created at same folder and package is not created:-
Note:?The imported class must be compiled before the actual class.
?Note: Example class compiled and p1 package is created .
?Note: Inside p1 package we have Example.class file which is generated after the compilation of Example.java file
Note:- After Example.java file compilation, We will compile Sample.java file.
Note: Both .class file created in p1 package folder.
?10. Creating classes at Different package level
Note: If we not created package folder so ,?imported class should be compiled first
Note: p2 package is created with Example.class file inside it, after compilation of Example.java file
Note: p1 package is created with Sample.class file inside it,?after the compilation of Sample.java file
? -------------------------/-----------------------
Subpackage:- package inside another package, that means we created a java file and place it one of the package, in other word parent-child relation implementation-
Both the files are created where Sample.java belongs to p1 and Example.java belongs to p2 which is inside the p1.
Note:- if we were directly executing our main java file that is parent java file without executing sub package file, we will getting an error.
After compilation of Example.java p1 folder created and inside it p2 because Example.java belongs to p1.p2 and -d create both packages simultaneously , since Example.java belongs to p2 package therefore inside p2 Example.class file created.
Note: Executing main java file that is Sample.java, since p1 package folder is already created therefore Sample.class file will generated inside??existing package p1.?
?The program compiled successfully.
Note:- if we declare sub package java file as non-public, then it will treated as default and according to accessibility modifier default scope is with in package. Therefore we are getting error.
--------------------/------------------------
Practice=> Define Example class in Sample.java with "a" and "b" values as 15 and 16. Then compile and execute Sample.java file directly. Now Example.class is regenerated with Example class values defined in Sample.java- local preference.
Note:- here Sample$Example is the another .class file generated through sample.java, which representing the Example class file compiled code in the form of Sample$Example.class file
Now identifying the number of classes does created after compilation of?java file.
领英推荐
?Can we create classes with predefined class name?
Here in main method creation our created class is overlapping?the predefine class by which our main method is not able to execute.
Then how can we differentiate these two classes?
How to access over created method of same name as predefine class name.
We can use our created class by importing through package name were it is created.
How many way we can access other package classes from our package classes?
We can access them in two ways-
???1. By using fully qualified name
???2. By using import keyword
?What is "import" keyword?
We have 2 syntaxes to use import statement
What is the difference between above two import syntaxes?
Rule: import statement must be placed before all class definitions, and after package statement.
How many import statements are allowed in one Java file?
Rule: To access package members from another package class its members must be declared as public, else it leads to compile time error while access that member.
Accessing other package members:-
Case 1:-If user class is also defined in same?package, import statement and fully qualified name is optional.
Case 2:- Is the class is using from another package, either import or fully qualified name must be used. Else it leads to CE: cannot find symbol.
Fully Qualified name:-
What is the benefit we get in using import statement over fully qualified name?
If we do not use import statement, we must use package name every wherever we are using class name or constructor. This code is considered as redundant code and also code is not readable. To solve this problem SUN introduced "import" concept So. If we use import statement we no need to use package name in referring class name and its constructor.
Give a?scenario where both import and fully qualified name should be used?
Using Sub package members:-
Why sub package members are not imported, when we import parent package?
Static import:-
This feature is introduced in java 5 to import static members of a class.
By using this feature we can access all
=>First syntax allows to call all static members of the class.
=>Second syntax allows only to call the imported static member.
Ex:-
?import p1.* ??
?import p1.A; we can access only class A from p1 package.
import static p1.A.*;???
Import static p1.A.a;
?Import static p1.A.a;
???Accessing protected member:-
Note :-the class members which have?protected keyword in its creation statement are called protested members. Those members can be accessible with in package from all classes, but from outside package only in subclass that too only by using subclass object(This rule is only for non-static protected members. Static protected members can be accessible by using same class name or by using subclass?name).
Solution:- Access protected members in sub class that to with regular import statement not by static import.
Most Restricted Accessibility modifier- Private>Default>Protected>Public
1.class level {public, default} 2. Member level{All four are used}
At member level- At the member level, we can also use the public or default( package- private) just as with class level, and with the same meaning
Accessing non-static as well static member through static import concept.
Note:-To solve above compile time errors we must also import class with normal import statement as?"import p1.A;"
If the current class also contains the imported static member, how can we differentiate both of them?
Solution:-
Practice Que- Write a program to print data without using class name System. You should use only "out.println()" to print "hi", "hello", "hru?".
Java Source file structure-
In a java file we can achieve package statement, import statement, interface, abstract class, concrete class, final class, main method class and documentation.
All these are organized as shown below according to coding standards and compiler rules
In-built packages-
Since in above approach we are facing one similar kind of problem that is , Compiling each and every file separately??before using to overcome such problem Auto compilation came into picture.
Auto Compilation
In a project , we no need to compile every class java file. When we compile main method class java file, all classes java file those are accessing from this main method class are automatically compiled by compiler software.
The phenomena or algorithm, compiling internally using classes java file??automatically by compiler software is called auto compilation.
Auto-compilation algorithm?all the step?
In above project we should not write main method in every class, its not compile time error or runtime error, it is a wrong design because the classes A ,B, C, and D must be executed from class test.
So we should not define main method in these 4 classes. We should define all 4 classes public, then only we can access them from Test class.
Rule:- Default class cannot be access from other package. To access the class from outside package it must an should be public type.
The valid way to arrange the java files-
In all above approach we are having a problem related to files, to easily differentiate them. That is separating .class file from .java is very tough for sending project executable file to client.
So the right approach is we must save java files and class files with package folder in separate directories as shown below
Procedure:-
1. Create project folder with two sub-directories????"src" and "bin".
2. Inside src folder create package folder with its java files.
?3. Compile the java file?with -d option by given bin?folder path.
4. Then -d option will create package folder inside bin directory with the .class files generated.
?5. Then change directory path to bin folder and?execute class files. (or) send bin folder all?packages with its class file to client.
??This is the right procedure of creating packaged java?files and class files.
?Not only we are following, eclipse also follows same procedure if we use eclipse IDE for java file creation, it automates this packages creation for java file and class files separately in src and bin folders.
?Example:-
Naming Convention of a package as per projects standards.
1. Inside package name all character should be small letters. And package name should be as short as possible.
Company web domain reverse functionality.
Package statement Rule:- ?In a single?java file only one package statement is allowed also the package statement must be first statement the java file.
Conclusion:-
Thankyou For Reading!
Published by:?Sakshi Agrawal
Date?:?April 23,2022