aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interface error" Watch "interface error" New topic
Author

interface error

Ram Gopal
Greenhorn

Joined: Apr 06, 2007
Posts: 14
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?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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:


cmbhatt
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A "call" like System.out.println needs to be inside the body of a method (or a constructor, or initializer block). For example...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ram Gopal
Greenhorn

Joined: Apr 06, 2007
Posts: 14
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

Joined: Feb 28, 2007
Posts: 1707

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

Joined: Apr 06, 2007
Posts: 14
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

Joined: Feb 28, 2007
Posts: 1707
Try this modified code and then refer to some useful points I posted above:

Ram Gopal
Greenhorn

Joined: Apr 06, 2007
Posts: 14
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

Joined: Feb 28, 2007
Posts: 1707
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
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 06, 2007
Posts: 14
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

Joined: Apr 06, 2007
Posts: 14
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 06, 2007
Posts: 14
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

Joined: Feb 28, 2007
Posts: 1707
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface error
 
Similar Threads
need a little help in this class
Implementing an interface by changing the argument name
compilation error...
runtime error
Multithreading: bouncing balls problem