简体   繁体   中英

how to call function of scala class from main method?

i am learning as i could not install scala plugin in my laptop because its 32 bit so i am practising in command prompt. I have created below scala classes but i am not sure hot call function from main method.

class AccessTest
{
  def display():Unit= println("this is from accessTest")
}

Object Hello
{
val access  = new AccessTest();
access.display();
}

both classes are in com folder. i am not sure about creating package manually . can somebody help me on this?

you are doing spell mistake while creating the object. You are giving Object instead of object . Here is an example:

scala> class AccessTest {
     |   def display():Unit= println("this is from accessTest")
     | }
defined class AccessTest

scala> object Hello extends  App{
     | val access  = new AccessTest();
     | access.display();
     | }
defined object Hello

I hope it will help you.

Simply Write a main method in object and call the main method using object name .(dot) main method with Array("")

scala>object MynewObj{
def main(args: Array[String]){
println("Hello World")
}
}

defined object MynewObj

scala> MynewObj.main(Array(""))
Hello World

To call it from main method, you either need to extend App or define main method. And object of 'o' must be in small letter.

scala> class AccessTest{
     |   def display():Unit= println("this is from accessTest")
     | }
defined class AccessTest

scala> object Hello extends App{
     | val access  = new AccessTest();
     | access.display();
     | }
defined object Hello

scala> Hello.main(Array(""))
this is from accessTest

scala> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM