> I have the foollowing class:
>
[quoted text clipped - 31 lines]
>
> 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);
}
}
> I have the foollowing class:
>
[quoted text clipped - 14 lines]
> 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
[quoted text clipped - 3 lines]
>
> 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
Guy - 02 Feb 2007 13:45 GMT
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
Daniel Pitts - 02 Feb 2007 17:53 GMT
> 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
[quoted text clipped - 19 lines]
> 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.
Guy - 02 Feb 2007 19:06 GMT
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.
Tor Iver Wilhelmsen - 03 Feb 2007 09:15 GMT
> 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.
Guy - 02 Feb 2007 13:48 GMT
> > 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);
Lew - 02 Feb 2007 15:03 GMT
>>> 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