- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Access Fields Directly (not using getters)
Fri, 2011-12-30, 21:07
Hi All,
How can I force Scala to access a field directly and not use a getter method (without using private[this]) ?
See the example
class User(var id: Int, var name: String) {
def doSomething = {
println(id)
}
}
If I decompile the generated Java class, I see that the method doSomething() uses the accessor method id() to get the value of id.
Thanks,
Drew
Fri, 2011-12-30, 23:21
#2
Re: Access Fields Directly (not using getters)
If you declare the var as private[this] you will get direct field access from within the class, but of course you will not be able to access it from outside the class.
I don't understand why you care? The jvm will optimise out the
function call if it gets used enough to matter...
On 30 December 2011 20:07, Drew Kutcharian wrote:
> Hi All,
>
> How can I force Scala to access a field directly and not use a getter method (without using private[this]) ?
>
>
> See the example
>
> class User(var id: Int, var name: String) {
> def doSomething = {
> println(id)
> }
> }
>
> If I decompile the generated Java class, I see that the method doSomething() uses the accessor method id() to get the value of id.
>
> Thanks,
>
> Drew