java - How to deal with arrays of static objects? -


i'm having issue dealing static object types in parser i'm writing.

glob = new func("glob"); glob.addchild(new func("wrong")); system.out.println(glob.name); 

func static class i'm referencing in above code within main. when code run, printed text "wrong". i'm assuming making func static did causing there ever 1 func allowed, , it's being overwritten since can't create instances of func. there way around this? here's part of code declaration of func reference

static class func{   public func (string name){      //etc   } } 

this becoming issue because want able create nest of these objects use determining scope within parser. func have children, , idea child node 'variable' (here string) add first within itself, within parent, , on down line. creating children overwrited parent though.

update: people wanted more code func

static class func{ public static func[] children; public string name; public static func parent; private static int child_index, var_index; private static string[][] vars;     public func (string name, func parent){        children = new func[50];        //etc     }  } 

you're right did have static name. if remove that, worry vars/children arrays still continue overwritten, , removing gives me lot of 'non-static variables cannot referenced...' messages.

the static modifier on class doesn't same thing static on other entities. first of all, can't apply static top-level class @ all. it's useful class defined inside class:

public class outer {      public class inner {     ...     }      static public class nested {     ...     }  } 

the difference whenever create object of class inner, object "belongs", in sense, object of outer class. inner object contains reference outer object, , methods can reference fields of outer object belongs.

the nested class, however, more top-level class; main difference outside classes can refer outer.nested, can useful when want several different nested classes named nested. it's way avoid "polluting namespace" top-level class names, , make clear nested somehow closely related outer. also, because of java's rules visibility, nested class's methods can access private members of outer, outside top-level class can't do.

but doesn't mean can create 1 nested. if want class can have 1 object, use singleton pattern. (but think whether want , why; singleton patterns disdained programmers, because global variables, reduces flexibility make kinds of changes program in future.) (p.s. after trying read question more carefully, i'm not sure singleton want, , in fact i'm not clear @ on design supposed like.)


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -