Multiple classes and main methods in Java, as well as packages and namespaces

In eclipse The java file name must be the same as the class name Is this true in any case or only in eclipse? For the main method, it looks like the main function in C, but the question is, what if I have two classes that have their own main methods and can link them together? In addition, I can be in one Write two classes in a java file? I use eclipse in windows. Is it the same as the Linux version?

For the bag, my book tells me that the bag is a catalog Therefore, to specify the package to which the file belongs, do I just need to say "package my_pack"? Do I need to create one like / SRC / my_ Pack such a directory and put my Java files in it?

Third, what is a namespace? What is its relationship to packages and classes?

Solution

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

Yes, you can have two classes with static void main (string [] args) {...} methods in the same project, but only one is the main class of your project. You should decide which one is declared in the manifest file

Read more about:

> Multiple classes in a single Java file,each with a main method – unexpected behavior? > Specifying the Main class to run in a jar file from command line > How to setup Main class in manifest file in jar produced by NetBeans project

Yes, you can. As long as there is only one class in the file, the name of the class is the same as the file name:

TheClass. Java file

package edu.home.bean;

public class TheClass {
    class SomeClass {
    }
}

class AnotherClass {
}

Edit:

If public access to a class is not defined, it may have default or private access, depending on the location of the declaration In the above example, someclass will have private access and can only be accessed by theclass, while anotherclass can be accessed from any class in the same package This means that with edu home. Classes in different packages of beans cannot 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;
}

The advantage of Java is that the code is platform independent (unlike C or C), so as long as the Java virtual machine (JVM) is installed in the operating system, the code can be ported from windows to Linux, and vice versa Want to run / develop a java project

Yes, this book is right When you have a java project (such as myfirstjava project), you can set the source folder and package in this folder These packages will be subfolders of the 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

You can do this manually, or have your IDE (eclipse in this case) do it for you, as simple as creating packages and classes in packages

Java does not support namespaces, but uses packages to facilitate class management / relationships You can import a class from another package (the first form) or use a class with the full package name (the second form)

Examples of the first form:

package package1;

public class Package1Class {
}

package package2;

import package1.Package1Class;

public class Package2Class {
    private Package1Class x;

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

Examples of the second 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
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>