(Context: I'm coming from a Python world and just re-learning my way around Java. Go gentle on me!)
Imagine I'd like the following effect from my Java program:
> SpacelySprockets.getName() => "Spacely Sprockets, Inc."
> CogswellCogs.getName() => "Cogswell Cogs Corp."
I know the following isn't legal Java, but I'd imagine a structure along these lines:
public class Manufacturer {
public static String getName() { return COMPANY_NAME; }
}
public class SpacelySprockets extends Manufacturer {
public static String COMPANY_NAME = "Spacely Sprockets, Inc.";
}
public class CogswellCogs extends Manufacturer {
public static String COMPANY_NAME = "Cogswell Cogs Corp.";
}
(FWIW: In a Python program, when the classmethod getName()
function is executed, it has access to the calling class's bindings i.e. COMPANY_NAME
). So:
- Is something along these lines legal in Java?
- If so, what's the correct syntax and/or idiom?