r/javahelp • • Jul 03 '20

Homework Have some questions about basic Hello World app

package javaapplication1;

 
public class JavaApplication1 {

    public static void main(String[] args) {
      System.out.println("Hello world!");
    }
}

The code as it is runs fine but if I remove the "package" line I get this error "Could not find or load main class javaapplication1.JavaApplication1".

Similarly if I change the name of the public class I get the same error. Does that mean every Java program has to start with this "package" line and why exactly is it needed? I'm guessing it just points to the file on the computer that it has to find and run. And the other thing I was wondering is why can't the public class be any random name why does it have to be the name of the file?

17 Upvotes

9 comments sorted by

9

u/BigGecko1 Jul 03 '20

As for the package question: when your class file is inside a folder (package) within the project, you need to specify the name of that package, with that package line at the top.

When changing the name of the class, you also need to change the name of the .java file that contains the class' code, when they don't match you get an error.

2

u/anhinav Jul 03 '20 edited Jul 03 '20

for the package question u dont need the package line (java doesnt necessarily have any such rule) but your build may be structured in such a way that it requires a package line to identify the file.( if you are using an ide this package line may be compulsory but if just simply write some code in notepad without the package line and save the file and compile run the program u will not get any error) try it!

for the class name question- yes its a java rule that the name of the file must be equal to the name of the class declared as public,thats why there can be only one public class in a file . the reason for that is that the .java file after compilation gets converted to a .class file . which has the same same as the public class name.

hope it helped!

1

u/steumert Jul 03 '20

And the other thing I was wondering is why can't the public class be any random name why does it have to be the name of the file?

Because the Java Language Specification (JLS) says so. Top-Level types must be in a file with the same name as the type. There are several technical reasons for this - the compiler knows just by looking at the type where the file ought to be, the interpreter can do class loading easily (see a type, just load the .class file).

Does that mean every Java program has to start with this "package" line and why exactly is it needed?

Packages again map 1:1 to folders. You can program without packages. Its just not a good idea. Packages avoid name conflicts. If every program just used the default package, naming conflicts would be abundant.

If you change the package declaration, you also need to change the folder structure.

Every IDE worth the name IDE will automatically move files to you when you refactor. This is not something you typically need to concern yourself with as programmer when you use the proper tools.

1

u/moe87b Jul 03 '20

The package is like a folder in which your class exists, if you go to the workspace or folder where your code is, you will see a folder with the package name. If you don't want to specify "package packagenamehere" then you have to move your class file outside the folder, to a folder named src which is the default package where you don't need to specify any package name.

It's discouraged to do so because when your code grows bigger it'll be useful to have a family of classes or classes that have something in common in the same package, it keeps everything organized.

Yes, the class name must be the name of the file in which it exists, but I don't know why. If you want to change the class name, you'll have to change the file's name. IDE s have an option to do that automatically for you, it's called refactoring, you change the class's name and the ide will change the file name for you, and all the references to that class in your project will be updated with the new name

0

u/VanVleet-goes-for-22 Nooblet Brewer Jul 03 '20
  1. Each .java file must have at most 1 “public” class. This public class must have the same name as the .java name.

  2. The package is where your class is.

Edit:

When you have put your Java source file into the correct directory (matching the package the class should belong to), you have to declare inside that class file, that it belongs to that Java package.

0

u/xyphanite Quantum Brewer Jul 03 '20

This has to do with how your build is structured. You don't necessarily need your Hello World to be part of a package. What IDE are you using (are you using an IDE?)? Please provide more details of your environment and we can better help you.

0

u/DS8KUHD Jul 03 '20

Omg something I could actually help with I think its cause the package is set via the files(it is under the javaapplictaion1 package) but if you delete the words from the text, it wouldn't be the same.