Dave's Brain

Browse - Programming Tips - Can can I have static and non-static in the same class?

Date: 2013nov29
Language: Scala

Q.  Can can I have static and non-static in the same class?

A.  If a class is all non-static use "class"

class MyClass { def method1() { } }
If a class is all static use "object"
object MyClass { def function1() { } }
If you want to have both use object and class together with the same name. This called a companion object:
object MyClass { var myStaticVar: Int = 9; def function1() { } } class MyClass { var myVar: Int; def method1() { // Call the static thing MyClass.function1(); } }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.