Help coderanch get a
new server
by contributing to the fundraiser
  • 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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a simple program to exercise the interface functionality in java.
I encountered one error. I wanted to make the interface public, but this gave me the following error: "class Driveable is public, should be declared in a file named Driveable.java." Why?

Here is the code:

Any help would be appreciated.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The .java file name must be the same as the public class' (or interface's) name.
 
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
To expand on wise owen's explanation...

A .java file can contain at most one public class or interface definition. If the file includes a public class or interface definition, then the name of the .java file must exactly match the name of that public class or interface.

Note that a .java file is not required to contain any public class or interface definitions, and can contain an unlimited number of non-public class or interface definitions.

Or to be precise...

When packages are stored in a file system (�7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

  • The type is referred to by code in other compilation units of the package in which the type is declared.
  • The type is declared public (and therefore is potentially accessible from code in other packages).
  • This restriction implies that there must be at most one such type per compilation unit.

    ...

    When packages are stored in a database (�7.2.2), the host system must not impose such restrictions.


    Ref: JLS - 7.6 Top Level Type Declarations.
    [ October 25, 2006: Message edited by: marc weber ]
     
    Laurie carrera
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Marc,
    If I understand you right, a .java file can have only one public declaration of the class/interface kind. The public interface therefore would have to be declared in a seperate .java file (if I am not wrong, the file should named after the interface, hence Drivable.java) and implemented from the test.java file.True/false?
     
    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

    Originally posted by Laurie carrera:
    ... True/false?


    True.

    If Drivable is going to be a top-level public interface, then it needs to be in a file called Drivable.java, which cannot include any other top-level public class or interface definitions.

    Similarly, if Test is going to be a top-level public class, then it needs to be in a file called Test.java, which cannot include any other top-level public class or interface definitions.
    [ October 25, 2006: Message edited by: marc weber ]
     
    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

    Originally posted by marc weber:
    ... A .java file can contain at most one public class or interface definition. If the file includes a public class or interface definition, then the name of the .java file must exactly match the name of that public class or interface.

    Note that a .java file is not required to contain any public class or interface definitions, and can contain an unlimited number of non-public class or interface definitions...


    I should add that I'm talking about top-level (non-nested) classes and interfaces here.
     
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    To extend the question:
    I think, if we are declaring any interface/class, it is not mandetory that the file name of the .java file should match the class/interface name.
    I.e. for the interface drivable, the name can be MyInterface.java. (we keep the name of the .java file and class inside it same for our convinience and to streamline the coding methodology)

    Are there any exceptions to this?
    I.e. are there any situations when it is enforced that the name of the .java file should exactly match the name of the class/interface in it? I think not...

    Thanks in advance.
     
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Abhijit Kolhatkar:
    To extend the question:
    I think, if we are declaring any interface/class, it is not mandetory that the file name of the .java file should match the class/interface name.
    I.e. for the interface drivable, the name can be MyInterface.java. (we keep the name of the .java file and class inside it same for our convinience and to streamline the coding methodology)

    Are there any exceptions to this?
    I.e. are there any situations when it is enforced that the name of the .java file should exactly match the name of the class/interface in it? I think not...

    Thanks in advance.



    Your question has already been answered in this thread. If you reread it carefully, you will learn that there is a quite important exception...
     
    Attractive, successful people love this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/t/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic