• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1928
  • Last Modified:

understanding package and classes, very general or maybe not ?

I am jumping in the deep end i know. I am beggining java and have started Netbeans. for every class I make must  i create a new file. And what does the package statement mean. How do iinclude classes into a package. If you could do a dog or cat objects and put it all in a package that may help give me instant understanding.
  But could you provide at leasrt some simple code with your reply.
thx.
0
rigidigital
Asked:
rigidigital
  • 3
  • 2
3 Solutions
 
srikanth1Commented:
Name conflicts are a major problem when you're developing reusable code. No matter how
carefully you pick names for classes, someone else is likely to use that name for a different
purpose. If you use simple, descriptive names, the problem gets worse since such names are
more likely to be used by someone else who was also trying to use simple, descriptive names.
Words like list, event, component, and so on are used often and are almost certain to
clash with other people's uses.
1533921
The standard solution for name collision in many programming languages is to use a package
prefix at the front of every class, type, global function, and so on. Prefix conventions create
naming contexts to ensure that names in one context do not conflict with names in other
contexts. These prefixes are usually a few characters long and are usually an abbreviation of
the product name, such as Xt for the X-Windows Toolkit.
1533921
When code uses only a few packages, the likelihood of prefix conflict is small. However, since
prefixes are abbreviations, the probability of a name conflict increases with the number of
packages used.
1533921
The Java programming language has a formal notion of package that has a set of types and
subpackages as members. Packages are named and can be imported. Package names are
hierarchical, with components separated by dots. When you use part of a package, either
you use its fully qualified namethe type name prefixed by the package name, separated by
a dotor you import all or part of the package. Importing all, or part, of a package, simply
instructs the compiler to look in the package for types that it can't find defined locally.
Package names also give you control over name conflicts. If two packages contain classes
with the same name, you can use the fully qualified class name for one or both of them.
1533921
Here is an example of a method that uses fully qualified names to print the current day and
time using the utility class Date (documented in Chapter 22), which, as with all time-based
methods, considers time to be in milliseconds since the epoch (00:00:00 GMT, January 1,
1970):
1533921
class Date1 {
public static void main(String[] args) {
java.util.Date now = new java.util.Date();
System.out.println(now);
}
}
And here is a version that uses import to declare the type Date:
1533921
import java.util.Date;
class Date2 {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now);
}
}
When the compiler comes to the declaration of now it determines that the Date type is
actually the java.util.Date type, because that is the only Date type it knows about.
Import statements simply provide information to the compiler; they don't cause files to be
included into the current file.
1533921
The name collision problem is not completely solved by the package mechanism. Two
projects can still give their packages the same name. This problem can be solved only by
convention. The standard convention is to use the reversed Internet domain name of the
organization to prefix the package name. For example, if the Acme Corporation had the
Internet domain acme.com, it would use package names starting with com.acme, as in
com.acme.tools.
1533921
Classes are always part of a package. A package is named by providing a package declaration
at the top of the source file:
1533921
package com.sun.games;

class Card {
// ...
}
If a package is not specified via a package declaration, the class is made part of an unnamed
package. An unnamed package may be adequate for an application (or applet) that is not
loaded with any other code. Classes destined for a library should always be written in named
packages.
0
 
rigidigitalAuthor Commented:
Thankyou. But also can you have multiple classes within one file ?

regards Mike.
0
 
willyRDCommented:
There are two cases where you can have multiple classes within one file:

1) You can have as many classes as you want in a source file, but only one of them (or none) can be public. The name of the file must match the name of the public class. If no public class is declared then the name of the file could be whatever you like.

example:

Dog.java
----------
public class Dog {
   ...
}

class PrivateDog {
   ...
}

2) You're declaring inner classes in your class. You can think of a inner class as a class that is part of the outer class.

example:

Dog.java
----------
public class Dog {
      
      class InnerDog {}
}
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
rigidigitalAuthor Commented:
if you were to suggest a book to learn java and netbeans what if any would you think the best. Any opinion appreciated. I saw 'The Java Tutorial Fourth Edition' on amazon.com

I think it is everything for the newbie which can be found on the sun web site.
I happen to like reading text books far more than the web!

But if this book does not also walk through making actual gui apps, such as I have seen in the best VB book authors  like McMillan or McMonnies then it would be not so great ?
0
 
willyRDCommented:
I have one book covering the main aspects of Java, it is a previous version of this one:
 
http://www.amazon.com/Certified-Programmer-310-055-Certification-Guides/dp/0072253606

(already recommended in http://www.experts-exchange.com/Programming/Languages/Java/Q_22905766.html)

I used it to get my Java certification and it covers the most important aspects of Java. The negative aspects are: parting from beginner level some chapters could be tough, and the Gui apps is not covered in a big depth.
0
 
rigidigitalAuthor Commented:
thanks for that link. May come in handy later. I used a similar style book for VB ,

'MCAD/MCSD Developing and Implementing Windows Based Apps with Visual Basic.Net and VS.Net' Training Guide.

I never did do any exams or certification but it was a useful reference book. Not so great to learn from scratch.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now