Java - Can we declare object variable in constructor? -


while creating object, can declare , initialize object variable in constructor?

i creating object based on xml. xml format output of previous system , dynamic.

hence object variable based on elements. better if declare variable in constructor.

in below code can declare temp object variable.

for example:

public class {     public a()     {         hashmap<string, object> temp = new hashmap<string, object>;     } } 

i not find sample code ideology.
suggestions?

i think you're looking bit different.

public class {    // constructor    private a() {    }     public static createobjecta(...) {       result = new a();       // in here transform xml in suitable format , set result object.       // return result       return result;    } } 

let's examine code above. have constructor can invoked within class, you're sure nobody use in bad manner.

then have static function provides instance of object a, you'll create every instance of through via:

a newobj = a.createobjecta(...); 

you can add many static functions allocate object matching number of various input formats have, advantage of approach resulting object free of logic parse previous input.

ofc can put static function inside class, you'll have make constructor callable (what friend functions in c++).


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -