alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (t1800.scala)

This example Scala source code file (t1800.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

array, array, objectholder, objectholder, privateobject, privateobject, test, test

The Scala t1800.scala source code

object ObjectHolder {
    private[ObjectHolder] class PrivateObject
    def getPrivateObject = new PrivateObject
}

object Test {
    def main(args: Array[String]) {
        // compiler error: class PrivateObject cannot be accessed
        // in object test.ObjectHolder
        val a: ObjectHolder.PrivateObject = ObjectHolder.getPrivateObject

        // works fine
        val b = ObjectHolder.getPrivateObject
        println(b.getClass)
    }
}
/*
When declaring objects as private[package/object] or protected[package/object] it is possible to leak out references to these objects into the public api (can be desirable, this in itself is not a problem).

When users of the api receive such private object via a function call, they can create a variable to reference the private object using inferred typing:

val b = getPrivateObject()

However they cannot create such variable using declared typing:

val a: PrivateObject? = getPrivateObject()

The line above will generate a compiler error: "class PrivateObject? cannot be accessed". Which makes sense, because PrivateObject? was declared private. But in this case inferred typing should not work either, otherwise the behaviors of inferred typing and declared typing become inconsistent. */

Other Scala examples (source code examples)

Here is a short list of links related to this Scala t1800.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.