• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Identifier Expected

 
Ranch Hand
Posts: 144
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

java intermediate
look at the following code
when I am tryng to comppile this using javac points/*.java
it says identifier expected at line A
My question is that A and B are similar so why this error and how to remove it .There is some silly point I am missing.But I dont know What ...
package points;
class Point{
protected int x=1,y=2;
System.out.println(x); ///A//////
System.out.println(y); ///B////
}
class fultu extends Point{
public static void main(String args[]){
Point p=new Point();
System.out.println(p.x);
System.out.println(p.y);
}
------------
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a rule, you are not permitted to make method calles outside of code blocks. You are trying to call the method Syste.out.println in the declaration portion of your class Point. javac is exiting when it finds the first error if you fixed //A and tried again, you'd get the error on //B.
If you really wish to see this on initialization, you could do the method calls in a default constrictor or a static initializer block.
default constrictor

initializer block

The primary difference is when each is called. Run a few tests with each and see if you can figure out what makes the two different.
[code]
 
sunil choudhary
Ranch Hand
Posts: 144
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Carl,
Perhaps I got what you wanted to say.
the variables of class Point are not accessible to fultu unless they are declared static. and that we cant make method calls outside code block.
Hope I am right .
(Sorry for the delayed post,I hope I am not late)
the following program works as anticipated ...

class Point{
static protected int x=1,y=2;
}
class fultu extends Point{
public static void main(String args[]){
System.out.println(x); ///A//////
System.out.println(y); ///B////
Point p=new Point();
System.out.println(p.x);
System.out.println(p.y);
}
}

thanks Carl
[This message has been edited by sunil choudhary (edited January 25, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic