Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2807
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Compile error: java: <identifier> expected |
| Date | 2013-10-14 10:11 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <l3gu2m$k7r$1@dont-email.me> (permalink) |
| References | <29442c5a-69a2-451a-88f0-25a5e0b9937e@googlegroups.com> |
On 10/14/2013 9:33 AM, Marc B wrote:
> Hello,
>
> I am new to Java. I create a reference variable, and then call a final method on that variable but on compilation I get this error:
>
> java: <identifier> expected
>
> pointing to the last line - p.sing();
>
> This is my code:
>
> class MyClass{
> public static void main(String[] args){
>
> }
> }
>
> class Person {
> final void sing() {
> System.out.println("la..la..la..");
> }
> }
> class Professor {
> final void singalong() {
> System.out.println("la..la..la..");
> }
> Person p = new Person();
> p.sing();
> }
>
> What does the error mean? What identifier needs to be present on that line p.sing();???
The message is confusing, because it complains about a by-
product of the actual mistake and not about the mistake itself.
(This often happens with error messages because "the" error
depends on what "the" purpose is understood to be, and you and
the computer don't always share the same notion of "the" purpose.)
In any event, "the" "real" problem is that the `p.sing();'
line is not inside a method (or a constructor, or an initializer
block). You can't just sprinkle free-floating executable statements
willy-nilly around your code; they've got to be contained within
methods (etc.). Your Professor class has one method, named
singalong, and that method contains one executable statement:
the println() call. The remaining lines are outside the method,
and that's why the compiler is unhappy.
Why did it complain about `p.sing();' and not about the line
before it? Because that line is a declaration of the variable `p',
plus an initialization of it. Declarations (and initializers)
*are* permitted outside methods; that's how you declare the member
variables of a class. In your case, every Professor object owns
a `p' variable, initialized to a fresh Person instance when the
Professor itself is created. That's probably not what you meant --
but it *does* have meaning, so the compiler didn't squawk (see the
earlier remarks about "the" purpose).
--
Eric Sosman
esosman@comcast-dot-net.invalid
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
Compile error: java: <identifier> expected Marc B <marc.at.compass@gmail.com> - 2013-10-14 06:33 -0700
Re: Compile error: java: <identifier> expected Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-10-14 10:11 -0400
Re: Compile error: java: <identifier> expected Marc B <marc.at.compass@gmail.com> - 2013-10-14 08:23 -0700
Re: Compile error: java: <identifier> expected Roedy Green <see_website@mindprod.com.invalid> - 2013-10-21 20:33 -0700
Re: Compile error: java: <identifier> expected abiolaquadri1987@gmail.com - 2014-11-07 07:07 -0800
csiph-web