Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1569

Re: Global enums and constants

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: Global enums and constants
Date 2012-02-15 09:38 -0800
Organization http://groups.google.com
Message-ID <10392490.302.1329327531503.JavaMail.geo-discussion-forums@pbcwl10> (permalink)
References <f3ad4f4b-a0b0-418e-b859-9bea0e084dda@m5g2000yqk.googlegroups.com> <UBB_q.3038$la5.847@newsfe17.iad> <ddd2aaca-22b4-49e5-8d18-96a2c0175fa6@f30g2000yqh.googlegroups.com> <W5R_q.21136$4K5.16082@newsfe16.iad>

Show all headers | View raw


On Wednesday, February 15, 2012 8:31:49 AM UTC-8, Daniel Pitts wrote:
> On 2/14/12 3:10 PM, Davej wrote:
> > On Feb 14, 4:53 pm, Daniel Pitts
> > <newsgroup.nos...@virtualinfinity.net>  wrote:
> >> On 2/14/12 2:37 PM, Davej wrote:
> >>> I'm having trouble accessing constants. I know it is always best to
> >>> define a constant in only one place, but public or private I still
> >>> can't see it. What am I doing wrong? Thanks.
> >>
> >> The problem is on line 15 of the example you posted.
> >>
> >> In other words, we don't know what you've tried, what error messages you
> >> see, or what you are really attempting to do.  Please provide an SSCCE
> >> <http://sscce.org/>  and then we'll be able to help without the use of
> >> clairvoyance.
> >
> >
> > Well, I had a constant CLASSFILEPATH defined here...
> >      public final String CLASSFILEPATH = "/WEB-INF/ClassList.txt";
> That is not exactly a constant. It is an instance field. You're missing 
> the static modifier.

Being 'static' is not part of the definition of a Java constant variable.
"We call a variable, of primitive type or type String, that is final and 
initialized with a compile-time constant expression (ยง15.28) a constant 
variable."
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.4>

So that is exactly a constant (variable).

The problem is that not being a static variable, the OP must reference it 
through an instance reference, which he did not do.

> This should do the trick:
>     public static final String CLASSFILEPATH = "/WEB-INF/ClassList.txt";
> 
> When you add "static", it means there is exactly one CLASSFILEPATH per 
> Student "class". Without the static it is per Student instance.

So the OP has two choices - reference the constant variable through an 
instance, if he keeps it an instance variable, or through the class, if he 
changes it to a static variable.

> >   ___________________________________________________
> >
> > Now note that the first file has an import for the second file. Still
> > the
> > constant cannot be seen inside the servlets package. The error is;
> >
> > error: cannot find symbol
> >    String path = sc.getRealPath(CLASSFILEPATH);
> >    symbol:   variable CLASSFILEPATH
> >    location: class CalculateGradesServlet
> > 1 error

Now take Patricia's advice.

-- 
Lew

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Global enums and constants Davej <galt_57@hotmail.com> - 2012-02-14 14:37 -0800
  Re: Global enums and constants Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-02-14 14:53 -0800
    Re: Global enums and constants Davej <galt_57@hotmail.com> - 2012-02-14 15:10 -0800
      Re: Global enums and constants Patricia Shanahan <pats@acm.org> - 2012-02-14 15:24 -0800
        Re: Global enums and constants Davej <galt_57@hotmail.com> - 2012-02-14 15:54 -0800
      Re: Global enums and constants Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-02-15 08:31 -0800
        Re: Global enums and constants Lew <lewbloch@gmail.com> - 2012-02-15 09:38 -0800
          Re: Global enums and constants Roedy Green <see_website@mindprod.com.invalid> - 2012-02-15 14:52 -0800
            Re: Global enums and constants Lew <lewbloch@gmail.com> - 2012-02-15 16:28 -0800
              Re: Global enums and constants Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-02-15 21:30 -0400
  Re: Global enums and constants Patricia Shanahan <pats@acm.org> - 2012-02-14 15:04 -0800

csiph-web