Question : Dynamic naming of Java objects

I was thinking of making a menu and representing that meny by a class. I want the user to be able to make a new menu when ever he wants, and register what is needed in that menu. The problem is, that I guess I can't let the user decide the name of the object that represents the menu at runtime. So I guess that design is littlebit flawed. I then thought I could make an ArrayList and use it as a menu index with anonymous menu objects as shown in the code window(only the coded needed for the example is shown). But that didn't work. I was hoping to keep a track of the menu's in the menu index by they'r index number in the ArrayList.

I am getting:
MenuProgram.java:118: cannot find symbol
symbol  : constructor Menu(java.lang.String)
location: class Menu
                        menuIndex.add(new Menu(name));
                                      ^
1 error

I am interested in getting to know other's thoughts on this. What would be a better design to solve the problem? I guess I would have to create a class and corresponding object to keep track of all the menues. When I then want to create new menues, should they not be represented as objects(I then have the same problem, naming them)?
1:
2:
3:
4:
5:
6:
class MenuRegister {
	ArrayList<Menu> menuIndex = new ArrayList<Menu>();
	public void createMenu(String name) {
			menuIndex.add(new Menu(name));
	}
}

Answer : Dynamic naming of Java objects

you have written wrong syntax for your constructor. You should not have "void" in your constructor. Please correct it as below:

public Meny(String name) {
            this.name = name;
      }
Random Solutions  
 
programming4us programming4us