Constants class nickname problem

G

Guy

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?
 
D

Daniel Pitts

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);
}
}
 
L

Lew

Guy said:
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
 
G

Guy

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
 
G

Guy

I invoke it in my code as
Sorry that was a typo, the .Bean is required and is the whole purpose:
function( arg1, MediVisitInstallConst.Bean.CONFIG_MI,...etc);
 
L

Lew

Guy said:
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
 
D

Daniel Pitts

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.
 
G

Guy

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.
 
T

Tor Iver Wilhelmsen

Guy said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Void problem 1
Class Constants - pros and cons 46
Java matrix problem 3
Help in hangman game 1
Problem with reflection in C# 4
Java problem 1
My Status, Ciphertext 2
School Project 1

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top