1. Primary
2. Auxillary
The primary constructor is the entire body of the class. Any parameters that the constructor requires are listed after the class name.
Simple Primary Constructor
class PrimaryConstructor(msg:String) {
// The message body is the primary constructor
println(msg);
}
object Invoke extends Application {
new PrimaryConstructor("Hello From PrimaryConstructor")
}
Observation
javap of the PrimaryConstructor.class gives the below mentioned result.
Corresponding Java Class Looks like the one below
public class PrimaryConstructor extends java.lang.Object implements scala.ScalaObject
{
public PrimaryConstructor(java.lang.String);
}
Simple Primary Constructor With Val
Adding a Val in the constructor parameter creates a public accessors and a private field of that reference.
class PrimaryConstructorVal(val msg:String) {
// The message body is the primary constructor
println(msg);
}
object InvokeVal extends Application {
new PrimaryConstructorVal("Hello From PrimaryConstructorVal")
}
Observation
javap of the PrimaryConstructorVal.class gives the below mentioned result.
Corresponding Java Class Looks like the one below
public class PrimaryConstructorVal extends java.lang.Object implements scala.ScalaObject{
private msg;
public java.lang.String msg();
public PrimaryConstructorVal(java.lang.String);
}
Simple Primary Constructor with Var
If a parameter has the var keyword, a public writer method is also generated with the parameter’s name as a prefix, followed by _=. For example, if label were declared as a var, the writer method would be named label_= and it would take a single argument of type String.
class PrimaryConstructorVar(var msg:String) {
// The message body is the primary constructor
println(msg);
}
object InvokeVar extends Application {
new PrimaryConstructorVar("Hello From PrimaryConstructorVar")
}
Observation
javap of the PrimaryConstructorVar.class gives the below mentioned result.
Corresponding Java Class Looks like the one below
public class PrimaryConstructorVar extends java.lang.Object implements scala.ScalaObject{
public java.lang.String msg();
public void msg_$eq(java.lang.String);
public PrimaryConstructorVar(java.lang.String);
}
Simple Primary Constructor with Private Var/val
Adding the private keyword before the val or var keyword and the accessor methods won’t be generated.
class PrimaryConstructorPrivateVar(private var msg:String) {
// The message body is the primary constructor
println(msg);
}
object InvokePrivateVar extends Application {
new PrimaryConstructorPrivateVar("Hello From PrimaryConstructorPrivateVar")
}
Observation
javap of the PrimaryConstructorPrivateVar.class gives the below mentioned result.
public class PrimaryConstructorPrivateVar extends java.lang.Object implements scala.ScalaObject{
public PrimaryConstructorPrivateVar(java.lang.String);
}
Auxiliary Constructor
Auxiliary Constructors are Scalas way of handling multiple constructors with in a call.
1.First statement of auxiliary constructor must be a call to the another constructor of the same class and it should be declared before the calling constructor.
2.In Scala, super constructor can only be called from the primary constructor.
class AuxiliaryConstructor(wish:String,givenName:String,surName:String) {
// The message body is the primary constructor
def this(wish:String,givenName: String) = this(wish:String,givenName, "")
def SayHi() = println(wish+" "+givenName + " "+surName)
}
object InvokeAuxiliaryConstructor extends Application {
val constructor1 = new AuxiliaryConstructor("Hello From Contructor with Given Name","Joe")
constructor1.SayHi()
val constructor2 =new AuxiliaryConstructor("Hello From Contructor with Given/Surname Name","Steve","Meyer")
constructor2.SayHi()
}
Observation
javap of the AuxiliaryConstructor.class gives the below mentioned result.
//public class AuxillaryConstructor extends java.lang.Object implements scala.ScalaObject{
public AuxillaryConstructor(java.lang.String, java.lang.String);
public void SayHi();
public AuxillaryConstructor(java.lang.String, java.lang.String, java.lang.String);
}
Contructors
More About Constructors
Some More about Constructors

No comments:
Post a Comment