2

How can one create a package in Java:

In a book i read its :

package package_name public class whatever{} . . . 

But shouldn't this be enclosed in parenthesis such as :

package package_name { public class whatever{} . . . } 

Just a minor confusion. Can anyone give me an example of the correct syntax?

1
  • What makes you think it should be second one rather than first even if the book says first is correct? are you confusing it with C++ namespace or C#'s ? Commented Jun 9, 2013 at 11:24

4 Answers 4

4

The syntax for creating package

package package_name; public class Whatever { } 

You can find more useful info Here.

Sign up to request clarification or add additional context in comments.

Comments

1

When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.

Reference

Example : package illustration; <------------ import java.awt.*; public class Drawing { . . . } 

2 Comments

Your example shows an import statement, not a package declaration.
@ThorbjørnRavnAndersen Ahh.. my confusion.Thanks.
0

Package is created as follows

package package_Name;

This package name has to be the first statement in the file.Once you declare package name start defining methods or classes or interfaces in it.

If this package you have to use in your any java file then write

import package_Name;

so that all the methods defined in the package will be accessible in java file.

Comments

0

No parenthesies. Package is nothing else then just a folder or set of folders. For example, if you create package named: utils, new folder will be created in your source folder. If you create package named org.utils, two folders will be created in your source folder. org and utils which will be inside of org folder. Also, every next package which starts with org. (for example org.ui) will be just a new folder created in org folder. So your folder hierarchy will look like this:

org-- | utils | ui 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.