|| Packages in java Explanation ||

|| Java Packages ||

Q1.How to create a package?What will be stored inside a package?

Ans:- Inside a package .class files are stored.

  1. To create a package first write "package packageName" in the first line of the .java file.Then save the .java file .

  2. Now move to the terminal or command prompt and stay in the current working directory, use the command "javac -d . filename.java"

  3. 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:-

  1. You can use the Question1.(above) process every time for every .java file to available inside a class.

  2. Shortcut is write "package pacakgeName" in first line of each .java file save all those .java files.

  3. 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:

    1. Do the above process with only mention the package statement like : "package first1.second1.third1" in the first line of .java files .

    2. 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.