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
}
}