Constants class nickname problem

Discussion in 'Java' started by Guy, Feb 1, 2007.

  1. Guy

    Guy Guest

    I have the foollowing class:

    public class MediVisitInstallConst
    {
    public static class Bean
    {
    public static String CONFIG_MI = "beanConfig_MI";
    public static String DB_MI_ID = "beanInfraBD";
    public static String DB_MV_ID = "beanMediVisitBD";
    public static String DICT_MI = "beanDict_MI";
    public static String DICT_MV = "beanDict_MV";
    ....etc

    I invoke it in my code as
    function( arg1, MediVisitInstallConst.CONFIG_MI,...etc);

    It works, but I find the static class name rather long, and would to
    use a nickname for it;

    I try to declare:
    MediVisitInstallConst const;

    But when I try to use it:
    function( arg1, const.CONFIG_MI,...etc), the compiler complains:
    "unexpected type: required class, package, fond variable"

    The I try to "static" the whole class:
    public static class MediVisitInstallConst
    {
    public static class Bean
    {

    The compiler complains: "modifier static not allowed here"

    What am I doing wrong?
     
    Guy, Feb 1, 2007
    #1
    1. Advertising

  2. Guy

    Daniel Pitts Guest

    On Feb 1, 1:21 pm, "Guy" <> wrote:
    > I have the foollowing class:
    >
    > public class MediVisitInstallConst
    > {
    > public static class Bean
    > {
    > public static String CONFIG_MI = "beanConfig_MI";
    > public static String DB_MI_ID = "beanInfraBD";
    > public static String DB_MV_ID = "beanMediVisitBD";
    > public static String DICT_MI = "beanDict_MI";
    > public static String DICT_MV = "beanDict_MV";
    > ...etc
    >
    > I invoke it in my code as
    > function( arg1, MediVisitInstallConst.CONFIG_MI,...etc);
    >
    > It works, but I find the static class name rather long, and would to
    > use a nickname for it;
    >
    > I try to declare:
    > MediVisitInstallConst const;
    >
    > But when I try to use it:
    > function( arg1, const.CONFIG_MI,...etc), the compiler complains:
    > "unexpected type: required class, package, fond variable"
    >
    > The I try to "static" the whole class:
    > public static class MediVisitInstallConst
    > {
    > public static class Bean
    > {
    >
    > The compiler complains: "modifier static not allowed here"
    >
    > What am I doing wrong?


    Why do you have a nested class for constants?

    Why not

    public class MyConstants {
    public static final String myValue="myValue";
    }


    public class MyOtherClass {
    public MyOtherClass() {
    System.out.println(MyConstants.myValue);
    }
    }
    (or)
    static import MyConstants.*;
    public class MyOtherClass {
    public MyOtherClass() {
    System.out.println(myValue);
    }
    }
     
    Daniel Pitts, Feb 1, 2007
    #2
    1. Advertising

  3. Guy

    Lew Guest

    Guy wrote:
    > I have the foollowing class:
    >
    > public class MediVisitInstallConst
    > {
    > public static class Bean
    > {
    > public static String CONFIG_MI = "beanConfig_MI";
    > public static String DB_MI_ID = "beanInfraBD";
    > public static String DB_MV_ID = "beanMediVisitBD";
    > public static String DICT_MI = "beanDict_MI";
    > public static String DICT_MV = "beanDict_MV";
    > ...etc
    >
    > I invoke it in my code as
    > function( arg1, MediVisitInstallConst.CONFIG_MI,...etc);
    >
    > It works, but I find the static class name rather long, and would to
    > use a nickname for it;


    It surprises me that that works. I would've thought you'd need to use

    function( arg1, MediVisitInstallConst.Bean.CONFIG_MI, ... )

    I wonder why it lets you slide without the "Bean".

    > I try to declare:
    > MediVisitInstallConst const;
    >
    > But when I try to use it:
    > function( arg1, const.CONFIG_MI,...etc), the compiler complains:
    > "unexpected type: required class, package, fond variable"


    Maybe you need the "Bean"?

    > The I try to "static" the whole class:
    > public static class MediVisitInstallConst
    > {
    > public static class Bean
    > {
    >
    > The compiler complains: "modifier static not allowed here"


    You cannot declare a top-level class "static".

    What would that mean, anyhow? "static" means "class-level", so at what class's
    level would a top-level "static" class be?

    - Lew
     
    Lew, Feb 1, 2007
    #3
  4. Guy

    Guy Guest

    The reason I do this is, for one, because it works in C#.Net, and
    since .Net is a vulgar retro-fit of the Java ideas
    I can't beleive its not doable in Java.

    Second, I have several constants and I like to regroup them in the
    same package, no other constants declaration in any part of my code.
    Since I have several constant, I like to sub-group them in categories,
    in order to keep code simplicit.
    MyConst.Numeric.PI, MyConst.Html.Home, MyConst.Xml.Settings ...etc.

    I do the same with enums.

    I use a static class because I got tue idea from studing the
    "Singleton" pattern.

    So I .NET its simple:
    class static MyConst
    {
    public static class Xml
    {
    ...
    public static class Html
    ....etc
     
    Guy, Feb 2, 2007
    #4
  5. Guy

    Guy Guest

    > > I invoke it in my code as
    > > function( arg1, MediVisitInstallConst.CONFIG_MI,...etc);

    >


    Sorry that was a typo, the .Bean is required and is the whole purpose:
    function( arg1, MediVisitInstallConst.Bean.CONFIG_MI,...etc);
     
    Guy, Feb 2, 2007
    #5
  6. Guy

    Lew Guest

    Guy wrote:
    >>> I invoke it in my code as
    >>> function( arg1, MediVisitInstallConst.CONFIG_MI,...etc);

    >
    > Sorry that was a typo, the .Bean is required and is the whole purpose:
    > function( arg1, MediVisitInstallConst.Bean.CONFIG_MI,...etc);


    If you copy-and-paste directly from source you avoid typos.

    ..NET is not Java. Java is not .NET. In Java the "static" keyword means
    "belonga-class". A top-level class has no other class to which it can belong,
    ergo "static" makes no sense there.

    Other than that I see no problem with the scheme

    Constants.Bean.CONFIG_MI, Constants.Xml.SOMETHING, ...

    other than stylistic.

    > I try to declare:
    > MediVisitInstallConst const;
    >
    > But when I try to use it:
    > function( arg1, const.CONFIG_MI,...etc), the compiler complains:
    > "unexpected type: required class, package, fond variable"
    >


    Your "const" variable was also missing the "Bean" reference.

    Try "function( arg1, const.Bean.CONFIG_MI, ... )"

    > Second, I have several constants and I like to regroup them in the
    > same package,


    I see that you have them grouped in different nested classes within the same
    outer class, which trivially puts them in the same package.

    - Lew
     
    Lew, Feb 2, 2007
    #6
  7. Guy

    Daniel Pitts Guest

    On Feb 2, 5:45 am, "Guy" <> wrote:
    > The reason I do this is, for one, because it works in C#.Net, and
    > since .Net is a vulgar retro-fit of the Java ideas
    > I can't beleive its not doable in Java.
    >
    > Second, I have several constants and I like to regroup them in the
    > same package, no other constants declaration in any part of my code.
    > Since I have several constant, I like to sub-group them in categories,
    > in order to keep code simplicit.
    > MyConst.Numeric.PI, MyConst.Html.Home, MyConst.Xml.Settings ...etc.
    >
    > I do the same with enums.
    >
    > I use a static class because I got tue idea from studing the
    > "Singleton" pattern.
    >
    > So I .NET its simple:
    > class static MyConst
    > {
    > public static class Xml
    > {
    > ...
    > public static class Html
    > ...etc



    It would be much better to "group" the constants with the class that
    makes the most use of them, rather than in some other "constant only"
    class.

    Think of it this way. In OO design, a "class" defines the behaviour
    and interface of a type of object. This object should be self-
    contained with its data and behaviour. Putting the constants in a
    seperate class would be like writting your name in your underwear so
    you can look it up when you forget. You know your name, so you don't
    need to store it in a different object.
     
    Daniel Pitts, Feb 2, 2007
    #7
  8. Guy

    Guy Guest

    You are right. But I am using this technique because the constants and
    enums are global to my application, and are needed by other classes.
    My application is in Install Shield 11.5 Java edition, the swing
    panels and frames contains controls which the user select, un-select,
    filll with passwords, installation paths...etc. This guide the
    behavior of the rest of the installation, so a further panel needs
    access to the string ID of these global objects so they can be
    retreive the object repository of IS.

    Of course if some constants have only a relevance into one class, then
    they stay in the scope of that particular class.

    Have a nice day.
     
    Guy, Feb 2, 2007
    #8
  9. "Guy" <> writes:

    > The reason I do this is, for one, because it works in C#.Net, and
    > since .Net is a vulgar retro-fit of the Java ideas


    It's more of a merge of C++ and Delphi (Borland's Object Pascal
    dialect), with glances to Java. The C# author was lured to Microsoft
    from Borland.
     
    Tor Iver Wilhelmsen, Feb 3, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiggaz
    Replies:
    6
    Views:
    32,206
    Raghudubba
    Nov 5, 2009
  2. TJS

    constants in code class

    TJS, Nov 5, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    313
  3. -
    Replies:
    6
    Views:
    447
    Wibble
    May 7, 2005
  4. E11
    Replies:
    1
    Views:
    4,901
    Thomas Weidenfeller
    Oct 12, 2005
  5. Vikas
    Replies:
    1
    Views:
    320
    Stephan Br?nnimann
    Sep 10, 2004
Loading...

Share This Page