java - How can I initialize interdependent final references? -


i have class , factory function creates new anonymous class objects extending class. however, anonymous class objects have method in there references other objects. in full program, need create , combine parsers, i've stripped down code here.

class base{     public base run(){         return null;     }        static base factory(final base n){         return new base(){             public base run(){                 return n;             }         };     } }  public class circularreferences{     public static void main(string args[]){         final base a, b;         = base.factory(b);         b = base.factory(a);     } } 

i circularreferences.java:17; error: variable b might not have been initialized. that's true, wasn't, can't set aside space these variables , initialize them using references these spaces, filled proper values before ever used? can perhaps use new separately constructor? how can create these variables reference each other?

the quick answer can't this. if absolutely must this, use private setter on class , bind things after constructed (i.e. use enforced immutability instead of final fields). it's obvious don't think idea - wanted provide answer actual question before answer way want to.

ok - out of way, here's real response called here:

generally speaking, sort of situation strong indicator refactoring needed separate concerns. in other words, base class trying responsible many things.

i realize example contrived, think functionality requires circular dependency, factor that functionality out separate class/object gets passed both of base constructors.

in complex architectures, circular dependency chains can pretty big, strictly forcing final fields great way types of refactoring opportunities.

if have concrete example, i'd happy refactoring suggestions break dependency this.


concrete example provided - here's suggestion:

it seems there concern of obtaining appropriate parsestrategy based on token. parsestrategyprovider. there toplevelparsestrategy reads next token, looks appropriate parse strategy, , executes it.

toplevelparsestrategy hold final reference parsestrategyprovider.

the parsestrategyprovider need have registration method (i.e. registerstrategy(token, parsestrategy) ).

this isn't functionally different doing enforced immutability via private setter (the registerstrategy method intents , purposes same private setter), design more extensible.

so you'd have:

public parsestrategy createparser(){   parsestrategyprovider provider = parsestrategyprovider.create();   toplevelparsestrategy toplevel = new toplevelparsestrategy(provider);   provider.registerstrategy("(", toplevel);    // create , register of other parse strategies    return toplevel; } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -