|| Packages in java Explanation ||
Q1.How to create a package?What will be stored inside a package?
Ans:- Inside a package .class files are stored.
To create a package first write "package packageName" in the first line of the .java file.Then save the .java file .
Now move to the terminal or command prompt and stay in the current working directory, use the command "javac -d . filename.java"
Now you can observe that inside your projectfolder your package has been built and inside that package a .class file is also available.
Q2.How to create a package containing multiple .class files.
Ans:-
You can use the Question1.(above) process every time for every .java file to available inside a class.
Shortcut is write "package pacakgeName" in first line of each .java file save all those .java files.
Now go to the command prompt or terminal and write the command "javac -d . .java" now all the files will go to the respective packages you have mentioned in the .java files and inside the packages respective .class files of .java files are available.
Q3.How to create pacakage inside another package which is inside another package and so on.
Ans:
Do the above process with only mention the package statement like : "package first1.second1.third1" in the first line of .java files .
Now you can observe that your .class files of .java files are available inside "third1" folder which is inside "second1" folder which is inside "first1" folder. /If you want to run this file by run button then make sure package statement is in comment,If package statement is not in comment then you have to run the file by giving the command.
Subscribe to my newsletter
Read articles from Rudraprasad Mohapatra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by