Companion objects in Scala

0 votes
Please explain what is the companion object. I'd appreciate if you shared an example too
Feb 23, 2019 in Apache Spark by Sharan
132 views

1 answer to this question.

0 votes

When a singleton object is named the same as a class, it is called a companion object. A companion object must be defined inside the same source file as the class. Here is an example:

class Main {

    def sayHelloWorld() {

        println("Hello World");

    }

}

object Main {

    def sayHi() {

        println("Hi!");

    }

}

In this class, you can both: instantiate Main and call sayHelloWorld() or call the sayHi() method on the companion object directly, like this:

var aMain : Main = new Main();

aMain.sayHelloWorld();

Main.sayHi();
answered Feb 23, 2019 by Uma

Related Questions In Apache Spark

0 votes
1 answer

Multidimensional Array in Scala

Multidimensional array is an array which store ...READ MORE

answered Feb 11, 2019 in Apache Spark by Omkar
• 69,030 points
584 views
0 votes
1 answer

where can i get spark-terasort.jar and not .scala file, to do spark terasort in windows.

Hi! I found 2 links on github where ...READ MORE

answered Feb 13, 2019 in Apache Spark by Omkar
• 69,030 points
382 views
0 votes
1 answer

what are the job optimization Technics in spark and scala ?

There are different methods to achieve optimization ...READ MORE

answered Mar 18, 2019 in Apache Spark by Veer
823 views
0 votes
2 answers

How to execute a function in apache-scala?

Function Definition : def test():Unit{ var a=10 var b=20 var c=a+b } calling ...READ MORE

answered Aug 5 in Apache Spark by Ramkumar Ramasamy
121 views
+1 vote
2 answers
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,950 points
5,840 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,950 points
872 views
+1 vote
11 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyF ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
36,703 views
0 votes
1 answer

Not able to preserve shuffle files in Spark

You lose the files because by default, ...READ MORE

answered Feb 23, 2019 in Apache Spark by Rana
204 views
0 votes
1 answer

Spark SQL in databricks

In sparkSql, we can use CASE when ...READ MORE

answered Feb 23, 2019 in Apache Spark by Rishi
757 views