Do I have to use “package” term in every class?

前端 未结 4 631
耶瑟儿~
耶瑟儿~ 2020-12-15 06:49

Firstly, I\'m trying to learn Java with Java, A Beginner\'s Guide, 4th Edition. I use Ubuntu as my OS and Netbeans as my IDE. I need to create a project to

相关标签:
4条回答
  • 2020-12-15 07:27

    Package represents a directory that contains related group of classes and interfaces.

    A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

    Below you can find some good discussions regarding java packages:

    • Java packages com and org
    • Are there best practices for (Java) package organisation?
    • Java com.* package namespace
    0 讨论(0)
  • 2020-12-15 07:31

    No you don't need to put in a package into your class UNLESS you are going to import it to another class that will be in it's own file. This is where protected type variables come in when you don't want to make them priviate but only want the subclasses (or child classes) access to them. You are also missing your public statement for your class so it should look like

    public class First{
        public static void main(String[] args){
              System.out.println("Hello!");
           }
    
    
    }
    
    0 讨论(0)
  • 2020-12-15 07:35

    You never need to put a class in a package. However, it is almost always a good idea. This is something that Netbeans aggressively tries to encourage you to do.

    For small projects, using the default package (that is, a file without a package statement) is OK. However, once your project grows and starts adding external libraries, bad things can happen. What if someone else decided to use the default package and happened to have an identically-named class? Now, since you're both in the same package, collisions can occur!

    Your file structure should also reflect your package. That is, a file in package com.myurl.helloworld should be located in a folder called com/myurl/helloworld. This is for the same reasons as above.

    Also, and you probably haven't gotten here in your studies, you cannot import classes from the default package, or use the package-private visibility modifier in a default package class.

    0 讨论(0)
  • 2020-12-15 07:42

    That's because the author of Java, A Beginner's Guide, 4th Edition most likely used the "default package". This way, you don't have to include any package. A package is a namespace declaration.

    What the heck is a namespace declaration!? A namespace declaration is simply a package which is made to organize your classes. For an instance, if you're going to have multiple classes for, let's say your GUI, and multiple classes for algorithms, blending them together is always a bit confusing. Sorting them in different packages, however is a superior solution.

    There is also a naming convention which you should follow if other people are going to look at your code. Packages should be named after a top-level domain. I tend to create SourceForge projects and then I end up with something like this:

    net.sourceforge.softwarename.security, net.sourceforge.softwarename.gui, etc...

    Also note that you should never use upper case when naming your package. More info here.

    You're going to encounter lots of situations like these when learning to programming. They're all a part of the game. You'll just have to figure out a bit by yourself.

    The best I can do for you is to recommend Eclipse. Also, when learning Java, I would suggest that you do not use an IDE at ALL! That's because you'll learn to code independently. It's up to you, though.

    0 讨论(0)
提交回复
热议问题