Main objective of a package is to keep files modularized and easy to be maintained. So we keep project files in several different folder or directories according to the namespace created, but sometimes we want to some variable, definations, classes or objects to be accessible to entire package. But its not possible to keep definations, data variables and type aliases directly in a package. To do so we have package object, so that the common data can be at the top level of the package. Package objects were introduced in scala version 2.8.
Syntax:
package projectx
package src
package main
object `package`
{
// using backticks
val x
// members
}
or standard way to do it is.
package src
package object main
{
val x
// members
}
In the above Syntax all the member of the package object are available to all the class in projectx.src.main package and this file would be saved as package.scala in the directory/package “main” which corresponding to the package name as well.
Example:
Lets have a package object GFG with path as follows:-
File Path: ../src/language/GFG/package.scala
package language
package object GFG
{
val fees = "fees"
val tax = "tax"
}
Another file GFG.scala with the main method in it.
File Path: ../src/language/GFG/GFG.scala
package language.GFG object GFG { val scala = "scala" val java = "java" val csharp = "csharp"} object demo { def main( args : Array[String]) { println(GFG.scala) println(GFG.csharp) var totalfees= tax + fees println(totalfees) } } |
Explanation:
As we can see that we have tax and fees directly available to use because GFG.scala and package.scala are at the same level.
+src
+language
+GFG.scala
+package.scala
// Scala program of package object // package eatables.food // Object created object GFG { val scala = "scala" val java = "java" val csharp = "csharp"} // Object created object demo { // Driver code def main( args : Array[String]) { println(GFG.scala) println(GFG.csharp) var totalfees = tax + fees println(totalfees) } } // package object created object `package` { val fees= "fees" val tax= "tax"} |
Output:
scala csharp taxfees
- They can extend other classes and or mixin traits.
package object main extends demovars { val x = a // from demovars val y = b // from demovars // members } - They are good place to store implicits rather than defining them in companion objects.
- Using package objects we can shorten our code by avoiding the use of imports.
package object main { val x = demovars.a // from demovars val y = demovars.b // from demovars // members } - Each package is allowed to have only one package object with the corresponding directory directory/package name.
+src +eatables +food.scala +package.scala // package object for eatables folder +drinkables +drinks.scala +package.scala // package object for drinkable folder
Recommended Posts:
- Chained Package Clauses in Scala
- Scala Singleton and Companion Objects
- UNIVERSAL Package in Perl Programming
- How to update dependency in package.json file ?
- R - Objects
- ES6 | Merge Objects
- How to merge two PHP objects?
- JavaScript | Date Objects
- Node.js | Global Objects
- Perl | Objects in OOPs
- Equality for two JavaScript objects
- Media Objects in Bootstrap with Examples
- How to implement a filter() for Objects in JavaScript?
- Understanding Classes and Objects in Java
- How to merge properties of two JavaScript objects dynamically?
- Sort array of objects by object fields in PHP
- How to remove duplicates from an array of objects using JavaScript ?
- R - Creating, Listing, and Deleting Objects in Memory
- How to remove Objects from Associative Array in JavaScript ?
- How to align objects vertically when working with grids in CSS ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

