I’m a C# programmer trying to hack at a Java project. Here’s an anonymized extract from our production code. It works (I think). Note that this is the whole class.
public class Y extends X
{
public Z m_Z;
protected void readCustomData (CustomStream reader, boolean forUpdate)
throws IOException, FTGException
{
super.readCustomData (reader, forUpdate) ;
m_Z.readBinaryData (reader, forUpdate) ;
}
protected void writeCustomData (CustomStream writer, int original)
throws IOException, FTGException
{
super.writeCustomData (writer, original) ;
m_Z.writeBinaryData (writer, original) ;
}
}
What puzzles me is – where is m_Z initialized? I cannot find it in the entire codebase. So why don’t the readCustomData and writeCustomData methods fail with NullReferenceException – or whatever the equivalent is in Java? Is m_Z somehow automagically constructed along with Y? Or have I missed something after all and there is some deeper magic in the codebase which initializes it?
When a Java class does not declare a constructor, the compiler implicitly adds a no-argument constructor that does nothing but call the superclass no-argument constructor (if there is none such, there will be a compiler error).
However, in your example the field
m_Zwould be null. If calls to those method succeed, then the field must be set elsewhere. It is public, after all (very bad practice).