How to make field in abstract class common in inner classes without making static?

Issue

This Content is from Stack Overflow. Question asked by Kookie

Suppose I have the following abstract class with nested static classes:

/**
 * Command class.
 */
public abstract class Command {
    public String commandName; // The name of the command.

    private Command() {}

    /**
     * Help command.
     */
    public static final class Help extends Command {
        public Help() { this.commandName = "help"; }
    }

    /**
     * Selection command.
     */
    public static final class Select extends Command {
        public final int choice;
        public Select(int choice) {
            this.commandName = "select";
            this.choice = choice;
        }
    }
}

I want the ability to access the field commandName from the Help and Select classes through Command.Help.commandName, for example. Unfortunately I can’t seem to do this because the commandName field is not static. Let’s say I “fix” this issue by making commandName static. The next problem arises when I run the following block of code:

Command c1 = new Command.Help();
System.out.println(Command.Help.commandName);
Command c2 = new Command.Select();
System.out.println(Command.Help.commandName);

This block outputs

help
select

when I would have expected

help
help

because I want the respective command names for Help and Select to stay fixed no matter what instances of either command I instantiate. Is the only solution to create a commandName field for every class, or is there a less repetitive way to do this?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?