3

In Eclipse the .java file name must be the same as the class name. Is it true in any case or only for Eclipse? For the main method, it looks like the main function in C, but the question is, what if I have two classes both with their own main method and is it possible to link them together? In addition, can I write two classes in a single .java file? I'm using Eclipse in Windows, is it the same as the Linux version?

For packages, my book tells me a package is sorta a directory. So to specify a package a file belongs to, do I just need to say "package my_pack"? Do I need to create a directory like /src/my_pack and place my .java file in it?

Third, what is a namespace? What's its relationship with packages and classes?

OneZero
  • 11,556
  • 15
  • 55
  • 92

3 Answers3

12

In Eclipse the .java file name must be the same as the class name. Is it true in any case or only for Eclipse?

The java file must have the same name as the public class that contains.

For the main method, it looks like the main function in C, but the question is, what if I have two classes both with their own main method and is it possible to link them together?

Yes, you can have two classes in the same project that have a static void main(String[] args) {...} method, but only 1 will be the main class for your project, and you should decide which one will be by declaring it in the manifest file.

Read more about this:

In addition, can I write two classes in a single .java file?

Yes, you can, as long as is just one class in the file and the name of that class is the same as the name of the file:

TheClass.java file

package edu.home.bean;

public class TheClass {
    class SomeClass {
    }
}

class AnotherClass {
}

EDIT:

When you do not define public access to a class, it could have default or private access, depending where is declared. In the example above, SomeClass will have a private access and can be accessed only by TheClass, while AnotherClass can be accessed from any class in the same package. This means, classes in a package different from edu.home.bean can't access AnotherClass.

package edu.home.control;

public class ControlClass {
    //this line compiles fine
    private TheClass x;
    //compilation error, it can't access for being in a different package
    private AnotherClass y;
    //compilation error, it can't access because its scope works only in TheClass
    private SomeClass z;
}

I'm using Eclipse in Windows, is it the same as the Linux version?

A good thing of Java is that the code is platform independent (not like C or C++), so you can port your code from Windows to Linux and viceversa, as long as the Java Virtual Machine (JVM) is installed in the operating system you want to run/develop Java projects.

For packages, my book tells me a package is sorta a directory. So to specify a package a file belongs to, do I just need to say "package my_pack"?

Yes, the book is right. When you have a Java project, like MyFirstJavaProject, you set a source folder and the packages inside that folder. The packages will be subfolders of your source folder. An example:

Project structure in Eclipse:

+ MyFirstJavaProject
  + src
    + edu.home.bean
    + edu.home.control
    + edu.home.gui
    + another.package


+ MyFirstJavaProject
  + src
    + edu
      + home
        + bean
          ... class files
        + control
          ... class files
        + gui
          ... class files
    + another
      + package

Do I need to create a directory like /src/my_pack and place my .java file in it?

You can do it manually, or let that your IDE (in this case, Eclipse) do it for you, as easy as creating packages and classes inside your packages.

Third, what is a namespace? What's its relationship with packages and classes?

Java doesn't work with namespaces, instead use packages to facilitate the manage/relation of classes. You can import classes from another package (1st form), or use the class with the full package name (2nd form).

Example of 1st form:

package package1;

public class Package1Class {
}

package package2;

import package1.Package1Class;

public class Package2Class {
    private Package1Class x;

    public Package2Class (Package1Class x) {
        this.x = x
    }
}

Example of 2nd form:

package package1;

public class Package1Class {
}

package package2;

public class Package2Class {
    //look that there is not use of the import statement, however everytime you need
    //to call Package1Class, you should provide the full package name
    private package1.Package1Class x;

    public Package2Class (package1.Package1Class x) {
        this.x = x
    }
}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

1) It is a Java thing. The file name must match the public class in the file. If there is no public class, you can name almost anything. If you have two classes with a main method, one can call the other. They are linked like in C. Just compiled against each other. You can have two classes in the same file; only one can be public. Eclipse is very similar across operating systems. There are a few differences to account for OS differences.

2) my_pack doesn't follow good naming conventions. my.pack or com.my is a better name. But yes, you want to create that directory.

3) Java doesn't use the term namespace, but I wikipedia says a package is like a namespace.

Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
  • What if no public class in a .java file? I mean, since no one is public, who is able to access these classes? For two classes both with main method, what do you mean "once can call the other"? Do you mean one main method can call another main like secondclass.main(), or after being compiled, one program can call another program to be run? If it is the former case, we want to only compile the first class, right? – OneZero Jun 06 '12 at 02:16
  • If no class is public, other classes in the same package can access it. One main method would contain SecondClass.main(args) in it which would call the other. You need to compile both at some point so the .class files exist. – Jeanne Boyarsky Jun 06 '12 at 02:19
  • But since both main methods can be run and both are compiled, how does the system know which one to be the real main? It sounds like two main functions in C and the system may mix them up. – OneZero Jun 06 '12 at 02:22
  • 1
    It runs the one you tell it too. If you run "java ClassOne", it runs the main method located in ClassOne. If you run "java ClassTwo", it runs the main method located in ClassTwo. – Jeanne Boyarsky Jun 06 '12 at 02:51
  • Just to add here, @user1229490 seems to be confused about how Java code is compiled. You see, each class, even if there are hundreds of them in a single file, gets compiled in its own .class file. Then, you tell `java` which one you want to run by specifying its name. The one you specified should have a `public static void main` method. – corazza Oct 21 '12 at 13:53
0

Its the rule in java to have the public class name same same as the .java file name ,irrespective of any IDE .

There can be multiple classes in a .java file provided that only one of them has the same name as that of public class.