• 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

interface error

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wrote a very simple small piece of code to test concepts abt interfaces.

public abstract interface Bounce
{
int v = 10;
}

class Ball implements Bounce
{

int x = v*2;

System.out.println("x : " +x);
}

And i get the following error:

c:\java\Ball.java:12: <identifier> expected
System.out.println("x : " +x);
^
1 error

Whats wrong?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Zack Snyder:
I just wrote a very simple small piece of code to test concepts abt interfaces.

public abstract interface Bounce
{
int v = 10;
}

class Ball implements Bounce
{

int x = v*2;

System.out.println("x : " +x);
}

And i get the following error:

c:\java\Ball.java:12: <identifier> expected
System.out.println("x : " +x);
^
1 error

Whats wrong?



Hi Zack,

Place both the statements inside the main() method
int x = v*2;
System.out.println("x : " + x)

Your complete program:
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "call" like System.out.println needs to be inside the body of a method (or a constructor, or initializer block). For example...
 
Ram Gopal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks .. i didnt notice that main method is missing..

but after including 'p.s.v.m' statement, i am getting compiler error, however the program runs giving expected output ' x : 20 '.

the compilation error is as follows:

c:\java\Ball.java:1: class Bounce is public, should be declared in a file named Bounce.java
public abstract interface Bounce
^
1 error

any reasons for such an error?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


but after including 'p.s.v.m' statement, i am getting compiler error, however the program runs giving expected output ' x : 20 '.



Are you sure what you are talking about?
Your class runs fine without compilation.


Anyways,
1- The name of the source file must match the name of the public class/interface name otherwise compilation error.

2- If your source file has no public class or interface, you can name your source file on your choice. While compiling you give the name of .java file;
Compiler creates .class file for each class that are in your source file with the name of the classes. Suppose your source file (somethig.java) has three classes A, B and C, compiler will create A.class, B.class, and C.class.

3- While invoking a class using java command, you must give the name of
class that has the main() method. Otherwise exception comes.

4- Per source file that must no be more than one public class or interface.

5- A .java source file can contain any number of non public classes and interfaces.
[ April 30, 2007: Message edited by: Chandra Bhatt ]
 
Ram Gopal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya you are rite....in fact this error seemed surprising.

in my above code the class name is Ball, so i saved the file as Ball.java

i compiled it using the command 'javac c:\java\Ball.java'

and ran the program by typing 'java Ball'

yet am getting an error when i compile as follows:

c:\java\Ball.java:1: class Bounce is public, should be declared in a file named
Bounce.java
public abstract interface Bounce

1 error

why so ?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this modified code and then refer to some useful points I posted above:

 
Ram Gopal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok... with this modification i am getting no error...

but what explains the earlier compile time error..

i mean the syntax was correct...

1) interfaces should be 'public' 'abstract'

2) class implements an interface

3) interface constants were implicitly 'public static final'

what surprised me that Bounce was an interface & Ball was a class, yet the error said that the class Bounce is public..
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ram Gopal:
ok... with this modification i am getting no error...

but what explains the earlier compile time error..

i mean the syntax was correct...

1) interfaces should be 'public' 'abstract'

2) class implements an interface

3) interface constants were implicitly 'public static final'

what surprised me that Bounce was an interface & Ball was a class, yet the error said that the class Bounce is public..



Actually you should have nammed your file name Bounce.java, because Bounce
is public there. You could have done this way too:

>javac Bounce.java
>java Ball
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,
What you specified in code was correct but i have a doubt.Look at the following code

public abstract interface Bounceable
{
}
public class Bounce implements Bounceable
{
}
Will the above code compile?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Java file can contain no more than one top-level class or interface definition that is public. (It is not required to contain any.) If there is a public top-level class or interface definition, then the file name must match that class or interface name.

So your example will compile if Bounceable (a public interface) and Bounce (a public class) are in separate files that are named accordingly. But it will not compile if these definitions are in the same file.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
Hi Chandra,
What you specified in code was correct but i have a doubt.Look at the following code

public abstract interface Bounceable
{
}
public class Bounce implements Bounceable
{
}
Will the above code compile?




NO!


4- Per source file there must not be more than one public class or interface. It means one public class or public interface; but there can be any number of non public class and interfaces in a .java source file.
 
Ram Gopal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:



NO!


4- Per source file there must not be more than one public class or interface. It means one public class or public interface; but there can be any number of non public class and interfaces in a .java source file.




hey wish to highlight the code which you modified

abstract interface Bounce // Modified line {
int v = 10;
}
public class Ball implements Bounce { //Modified Line
int x = v*2;
System.out.println("x : " +x);
}


In above code, although the interface hasnt been declared 'public', but by default it is 'public' implicitly. Also the class Ball is public.
i mean in this case, your 4th point (It means one public class or public interface) doesnt seem to apply....
 
Ram Gopal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry, i just noticed the difference...

1) All interfaces are implicitly 'abstract', whether the word 'abstract' is typed or not.

2) All interface METHODS are implicitly 'public' and 'abstract'.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
top level interface can be declared public, they are not implicitly public. If you don't give public modifier to interface it will have default access.

Same applies to top level classes;

1- Interfaces are abstract implicitly, so you can't make them final.
2- Interface methods are public abstract implicitly.
3- Interface constants are public static final implicitly.

"top level interfaces are not restricted to be public only"
 
Ram Gopal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah absolutely right...

infact i just tried to declare interface as private and it gives an error
"Bounceable1.java:1: modifier private not allowed here"

so i guess we can use ONLY public modifier for an interface...
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ram Gopal:
yeah absolutely right...

infact i just tried to declare interface as private and it gives an error
"Bounceable1.java:1: modifier private not allowed here"

so i guess we can use ONLY public modifier for an interface...



Yeah, We can only give "public" access modifier to the top level class or interface. No access modifier means it has default access.

Inner classes and interfaces can be declared public, private, protected as well.
 
reply
    Bookmark Topic Watch Topic
  • New Topic