java - Check if member is new-initialized -


how check in java if membervariable/memberarray (of every standard type int/double/float... etc) new-initialized? difference double/integer/float?

class class {     double[] memberarray;     double membervariable;      class() { }      void init()     {         memberarray = new double[12];         //membervariable = new double(); //edit: not compile     }      void foo()     {         // check here if membervariable/memberarray has been new-initialized     } } 

constructing these in ctor not option, arraysize isn't known @ point. , i'd use primitive types here, not container. example, not used in project.

there 2 basic types of variables given different default values member variables (when not member/instance variables not given default values @ all). 2 types reference variables , primitive variables.

reference variables 'refer' variables objects (anything not primitive). others primitives.

primitives given default values of 0, except boolean given default value of false. reference values given default value of null means not referring object.

so if want check whether reference variables (such array of doubles) have been initialized other value null can check whether null or not. so;

if(memberarray==null){      /* if hasn't been given non-default value */  } 

if want check whether primitives (such double) have been given non-default value can check whether different default. example;

if(membervariable==0) {      /* if it's still 0 */  } 

be warned though, because give them value same default later. alternatively java has wrapper classes allow use reference variables refer primitives, such double (instead of double. java case sensitive). way check whether double reference variables null.

as others have said though, better create constructor make sure initialized values.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -