Java Concepts...Layman terms

Hi, I actually hope any experts here can guide me by providing their ideas on my porblems. I promise for those who can explain to me in a layman for the below question, I will give them 100 points each. Thank you.

I have a JAVA programs. I need some groundwork or basic concepts to understand it correctly. I hope u can explain to me what is that Java all about.

I have a program (actually is a JavaBean), below is the code.

1 package;
3 import java.sql.*;
4 import java.sql.*;
6 public japf12 () {
7   String error;
8   Connection con;
10   public japf12 () { }
12   public void connect () throws .....

For the coding above,

1. Line 1 is my location of my JavaBean. Correct?
2. Line 3 and 4 is the API which I need to load for  
   furtehr reference in the coding i code inside this
   programs such as String, Connection. Correct?
3. Line 6 I think it's a constructor. But what is a
   constructor? Can anyone explain to me more layman?
4. Line 6, 7 is the decalration of the variable which
   adopt the functionality of the String and COnnection.
5. I don't have for the Line 10 and 12. Why must declare
   japf12 which don;t have code inside? Furthermore wht
    it's must exactly the same with the name of the

Thank you.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

1. Correct. This is also known as the 'package' that japf12 is in. Packages are containers for groups of related classes. A package is essentially a subdirectory.
2. Correct. These are other packages that contain classes that need to be loaded by japf12. String is in the java,lang package which is loaded automatically. Therefore you don't need to import it.

Back on 3,4 & 5 in a minute or 2!
1. Line 1 is the logic package that your class belongs to. Classes can have the same name, but they can be recognized by different package. For example: is different from com.freetest.lib.Question.
This also define the location of your class file. Package will be reflected in file system as a folder, and classes in a package are files. So will have this structure:
com (folder)
    expert-exchange (folder)
         sample (folder)
              Question.class (file)

2. Line 3 and 4 are identical, you can remove one. They declare the packages that you use a lot in your program, so you don't want to refer to the classes in these packages as full name.
For example, if you want to declare a variable of java.sql.Connection, you write:
java.sql.Connection mycon;
If you use java.sql.Connection (and other classes in package java.sql) alot, you can reduce the typing by adding an import statement:
import java.sql.*; //All classes in package java.sql
and then declare like this:
Connection mycon;
Java Compiler will look for class Connection (not found) and then java.sql.Connection (found) .. and replace Connection with java.sql.Connection.
So import is like auto complete functionality.

3. Line 6 should be:
public class japf12 {
it begins the declaration of class japf12.
Just a convention, class names should has the first character capitalized.

For information about constructor:

4. Line 7 and 8 declare 2 variables of class java.lang.String and java.sql.Connection. The variables don't create the objects of these classes yet, they just a handle (like a pointer) to these types of object. To create an object (which has the functionality of String), you write:
error = new String("testing");
Connection object cannot be created that way (because it's an interface). You can read JDBC document to learn how to use java.sql package.

5. Line 10 is the constructor of class japf12. You can remove it, java will automatically add a default constructor (with no parameter and does nothing). It must be the same name of the classname (indicates it's the constructor).
Line 12 is a normal method.
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.

3. Correct, although since there is something missing between lines 6 and 7, we'll discuss line 10, which is the so-called 'empty' constructor. What is a constructor? Let's consider a 'class' and an 'object'. If, for instance you had a 'Car' class and a 'Car' object, you could say that the class is like a blueprint or plan of a car and the object is like the car itself. You need the blueprint to create a car.
The constructor takes the blueprint and gives you the car.
What is happening in the machine is that the 'car' has been loaded into memory and its variables have been initialized. That 'Car' can then perform the functionality you have put into it.
Back to the 'empty' constructor. This is a contructor with no arguments. To use the car analogy again, you could have an empty constructor like:

public Car(){}

But what if you wanted to initialise some parts of the car in a certain way? Suppose you wanted to create a four door car or a two door car. You could do something like this:

public Car(int number_of_doors){}. The number of doors would then be saved in a variable to be used somewhere later.
The empty constructor will simply give you a plain car object. The important thing to remember is that the empty constructor is also know as the 'default' constructor, as it appears in the *class* file even if it is not delcared in your code. That is so you can always create a Car instance or object.

4. Correct, if your comments are applied to lines 7 and 8.

5. See answer to 3. If you have a single java class in a file, the source file name, before the .java extension, must be the name of the class in that file. So the source file would contain a class named Car.

Hope that helps
>  Line 6 should be: public class japf12 {

actually if it is a bean it should be serializable as well

public class japf12 implements Serializable {
foongkim1Author Commented:
Daiit, based on your information on question 3, the constructor manners, based on your understanding, can u explain to me. No doubt, I view the url u provided and understand what it;s dais, but I need to hear from your personall understanding.

And ofr the last comment (5) What is this contructor all about? U mentioned that it's optional for me to type it. And why it's must be the same name of my programs? ( I posted this question on my main question also).

Pierrecampe, do u mind tell me what is "ping" refering?

CEHJ, thanks for your fast respond on my question. But i am sorry, i don;t understand what is the object and what is the class. Based on  y undertansding, class is the functionality inside the programs and the object is the result of initialise variable from a pakages. Right? So sorry.......

bobbit31, what is serializables? I saw some of the sample applicatio from the internet also used this serialized.
1. Constructor is a method which will be called to construct an object. For example:
class Car{
   int weight = 10000;
   int seatcount;
   String type;
   String regid;
   //This is the constructor
   public Car(String manufacturer, String ID){
      seatcount = weight / 200;
      type = manufacturer;
      regid = ID;

To create an object of the class above, you write:
Car car = new Car("Ford","3F 3453");

What really happens when executing that line of code is:
- Allocate memory for the object and its attributes(weight, seatcount, type, regid).
- Initialize the variables (weight = 10000).
- Call constructor Car("Ford","3F 3453");
That's it.

So the constructor is the method to initialize the object before it's ready to use. Usually, constructor has no parameter (as in your sample code), but sometimes there must be some parameters in order to construct a complete object (as in my sample - assuming that a Car must have branch name and registration ID to be used in the rest of the program).

2. Constructor must have the same name with classname. It's just an impliciness, so the compiler knows which method is constructor, which is normal methods (if we don't agree with that, we must define another keyword just for constructor:
constructor public CarConstructor(...){}
which is not neccessary.
ping means just listening i'm interested
foongkim1Author Commented:
daitt, thanks for your detail explanation. I am very clear now. Thank you very much.
>>But i am sorry, i don;t understand what is the object and what is the class

This can be difficult to grasp at first, but I think the link below will help. You could think of the relationship between an object and its class as analogous to the relationship between apple pie and a recipe for apple pie.
Try this:
> what is serializables?

serialization refers to the process of converting objects to a stream of bytes that can be stored in a file or transmitted across a network. an object is serialized by being written to an ObjectOutputStream w/ the output streams writeObject() method.  It's received via an ObjectInputStream and then deserialized.

The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

so by doing something like:

public SerializableClass implements Serializable {

you are saying that the SerializableClass can be converted to a stream of bytes.

hope this helps... you can always look in the java docs for more information:
foongkim1Author Commented:
Thank you to all of you. I am really benefits a lot.

Before I end this, can anyone suggest some usefull websites whihch u think it's benefit for the ppl like me...such a dummies in this programming?

I already browse through JGuru, Sun. And no need to mention this 2.

foongkim1Author Commented:
daitt, thanks for the url privided.
Well, I think I have to close this discussion.

I have another one posted soon. Hope can see you all there. Regarding how to read the API....

For CEHJ, and babit31, please collect your points at the main Java Menu, i will create a special statement stated that this points is for you.

Thank you guys.
foongkim1Author Commented:
daiit, can u have for my other question in Java Programming regaadring the integer passing to Packed Decimal in DB2.

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.

All Courses

From novice to tech pro — start learning today.