Ce codePourquoi est-il permis de pointer vers les paramètres constructeurs?
class Foo(str: String) {
val len = str.length
def getLen = len
def getStr = str}
seront compilés à
public class Foo implements ScalaObject
{
private final int len;
private final String str;
public Foo(String str)
{
this.str = str;
super();
len = str.length();
}
public String getStr()
{
return str;
}
public int getLen()
{
return len();
}
public int len()
{
return len;
}
public int $tag()
throws RemoteException
{
return scala.ScalaObject.class.$tag(this);
}
}
Mais ce code
class Foo(str: String) {
val len = str.length
def getLen = len
}
seront compilés à
public class Foo implements ScalaObject
{
private final int len;
public Foo(String str)
{
len = str.length();
}
public int getLen()
{
return len();
}
public int len()
{
return len;
}
public int $tag()
throws RemoteException
{
return scala.ScalaObject.class.$tag(this);
}
}
Pourquoi il n'y a pas un privé membre de la classe Foo?
private final String str;
Est-ce une sorte d'optimisation? Pourquoi est-il permis de pointer vers les paramètres du constructeur?
Pourquoi? Pourquoi il n'y a pas d'erreur de compilation pour la ligne "def getStr = str
"?
+1 Ce matin, je me demandais la même chose :-). – helpermethod