18

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 create a class when using Netbeans.

Here is my hello world app.

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

But this returns a lot of errors. When I put package first; line to top line of my Java class it runs. But, the book isn't using package term. Is this a rule of Netbeans, or what I need to know about this?

gobernador
  • 5,659
  • 3
  • 32
  • 51
Yusuf
  • 219
  • 1
  • 2
  • 7
  • 1
    It is a namespace declaration, Java specific, not a Netbeans requirement. http://en.wikipedia.org/wiki/Java_package – Marek Sebera Jul 22 '13 at 21:44
  • 1
    Ok, I understand. When I move my class out of the package, I don't need to use package term. – Yusuf Jul 22 '13 at 21:51

4 Answers4

20

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.

gobernador
  • 5,659
  • 3
  • 32
  • 51
  • I reverted the suggested edit on this answer because it was not a substantial change and because someone coming to this answer to learn about packages might not know what is meant by the "default" package. – gobernador Jul 18 '18 at 18:32
12

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.

gobernador
  • 5,659
  • 3
  • 32
  • 51
Scientious
  • 337
  • 1
  • 8
  • 2
    You wrote a nice answer, but you didn't actually explain to the poor guy how packages work. Packages have been in Java since the beginning, so the book omitting it is not the problem of the book being old and outdated. And it won't be magically solved by switching to Eclipse (nor any other IDE). – Petr Janeček Jul 22 '13 at 21:59
  • 1
    Could you consider removing the dislike now? – Scientious Jul 22 '13 at 22:12
  • 2
    I would rather recommend using the same IDE that you will be using for the reason you are learning Java, that way you aren't just a monkey stuck with notepad while everyone else is highly productive. – ProfK Nov 24 '17 at 05:53
2

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!");
       }


}
1

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:

Community
  • 1
  • 1
tokhi
  • 21,044
  • 23
  • 95
  • 105