Scala | Package Objects


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
filter_none

edit
close

play_arrow

link
brightness_4
code

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)
  }
}

chevron_right


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
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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"
}

chevron_right


Output:

scala
csharp
taxfees
Package objects Important points
  • 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
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.