Link to home
Start Free TrialLog in
Avatar of foongkim1
foongkim1

asked on

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.

japf12.java
===========

1 package com.cars;
2
3 import java.sql.*;
4 import java.sql.*;
5
6 public japf12 () {
7   String error;
8   Connection con;
9
10   public japf12 () { }
11  
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.
   Correct?
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
    program??

Thank you.

 
 
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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: com.expert-exchanges.sample.Question 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 com.expert-exchange.sample.Question 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:
http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html

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.
Avatar of pierrecampe
pierrecampe

ping
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 Car.java 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 {
Avatar of foongkim1

ASKER

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
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:

http://java.sun.com/docs/books/tutorial/java/concepts/index.html
> 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:
http://java.sun.com/j2se/1.4/docs/api/java/io/Serializable.html
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.

Thanks.
 
ASKER CERTIFIED SOLUTION
Avatar of daitt
daitt
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
daiit, can u have for my other question in Java Programming regaadring the integer passing to Packed Decimal in DB2.

Please...