Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Constants class nickname problem

Reply
Thread Tools

Constants class nickname problem

 
 
Guy
Guest
Posts: n/a
 
      02-01-2007
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?

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      02-01-2007
On Feb 1, 1:21 pm, "Guy" <(E-Mail Removed)> 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);
}
}

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-01-2007
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
 
Reply With Quote
 
Guy
Guest
Posts: n/a
 
      02-02-2007
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

 
Reply With Quote
 
Guy
Guest
Posts: n/a
 
      02-02-2007
> > 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);

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-02-2007
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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-02-2007
On Feb 2, 5:45 am, "Guy" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Guy
Guest
Posts: n/a
 
      02-02-2007
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.

 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      02-03-2007
"Guy" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Must declare the scalar variable @Nickname"... Jiggaz ASP .Net 6 11-05-2009 07:01 AM
Yahoo original email having nickname recognition problems? pattyjamas@hotmail.com Computer Support 0 10-20-2006 10:19 PM
Yahoo, is ir posssible to change nickname? Adriano Computer Information 2 10-30-2005 01:45 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments