Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

understanding package and classes, very general or maybe not ?

Posted on 2007-10-20
6
Medium Priority
?
1,922 Views
Last Modified: 2013-12-15
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
Comment
Question by:rigidigital
  • 3
  • 2
6 Comments
 

Accepted Solution

by:
srikanth1 earned 1050 total points
ID: 20118106
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
 

Author Comment

by:rigidigital
ID: 20118956
Thankyou. But also can you have multiple classes within one file ?

regards Mike.
0
 
LVL 3

Assisted Solution

by:willyRD
willyRD earned 450 total points
ID: 20122327
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rigidigital
ID: 20122878
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
 
LVL 3

Assisted Solution

by:willyRD
willyRD earned 450 total points
ID: 20126964
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
 

Author Comment

by:rigidigital
ID: 20127019
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question