
In this Scala Tutorial, you will learn How to write your first Scala Hello World application by creating your first project and then running it.
Object is a class but it already has an instance, so you can not call new ObjectName. The object keyword creates a new singleton type, which is like a class that only has a single named instance. An object has exactly one instance (you can not call new MyObject).
You can have multiple instances of a class. Object serves the same (and some additional) purposes as the static methods and fields in Java.
object HelloWorld { /* This is first Scala program. * This will print 'Hello World' as the output */ def main(args: Array[String]) { println("Hello, world!") // prints Hello World } }
- Save on a file named HelloWorld.scala
- Compile with scalac HelloWorld.scala (or from your IDE)
- Run with scala HelloWorld (or from your IDE)
- Generate documentation with scaladoc HelloWorld.scala
- The above code creates a singleton object
- Everything in the object is like Java’s static
- Scala doesn’t have static
- Scala does have classes; we’ll get to those later
Leave a Reply