[program-java] Re: Java Tutorial 3.1

  • From: Anna Giller <anna.giller@xxxxxxxxxxxxxxx>
  • To: program-java@xxxxxxxxxxxxx
  • Date: Fri, 28 Nov 2008 13:51:06 -0500

Jim,

what is the reason of having 2 lines of code:
private HelloWorld hw = null;
hw = new HelloWorld();

Why can't we have just one:
private HelloWorld hw =new HelloWorld();

Thank you,
Anna

-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Corbett, James
Sent: Wednesday, November 26, 2008 1:39 PM
To: program-java@xxxxxxxxxxxxx
Subject: [program-java] Java Tutorial 3.1

In Tutorial 3.0 we essentially created a new package, a new class,
discussed public verses private, created a constructor method for our
class, and created a private member, a getter and a setter....

So, I'm trusting that if you have been following along you have compiled
the project to this point and have fixed all of your errors....

Now we have a class called HelloWorld.... In reality it still is as
useless to us / the world as it was when we first created it with out
any attributes or methods. We could go as far as to say it's a button
from your jacket that is sitting in the bottom of a drawer some where
and it never see's the lite of day. It's a class, meaningless dribble.
So what do we do with this lump of belly button putty. Well folks we are
going to create an object.

1.0     Let's open the project explorer.
2.0     Expand the tree and locate then open the Main.java file.
3.0     Add a couple of blank lines below the "package main;" line.
4.0     Add some comments either with the double slashes or the /* as in
the previous tutorial.

We need to discuss importing at this point.... Regardless of whether the
class you are importing is in the same package or other packages or even
other .jar files none of these classes are self aware of the existence
of other classes.... Much like a teenager watching TV, the house could
fall down around them and they wouldn't even notice.

...slightly off topic for a second, a package can be directly part of
your project much in the way we have been discussing it. It also can be
an external .jar file which may contain many packages. Some of these
.jar files are known to your IDE such as the Java classes and yet others
have to be manually added as reference packages to the project. Here are
several examples:

A.      import example_one.HelloWorld;
B.      import java.io.*;
C.      import org.eclipse.swt.widgets.MessageBox;

In the first example, we are directly specifying our package
"example_one" and the class "Hello World".

Example 2 imports from the java package, the io package and all packages
and or classes below io.

In the third example we stretch the import out burrowing down through a
number of packages to get at the MessageBox class.

Ok, clear as mud right, well you really don't have to do it this way...
you can actually with in your methods explicitly declare the class like
this:

Private example_one.HelloWorld hw = null;

Or

Example_one.HelloWorld hw = new example.HelloWorld();

These are all valid statements and each is useful in there own right....
More about that later.

So for our particular example we are going to import our package and
class below our comments and a line or two above the public declaration
of class Main.

Now just below the class declaration and above the constructor we need
to create a copy of the class:

Private HelloWorld hw;

Once we have done this we can create an instance of the class with in
the constructor of the class Main:

hw = new HelloWorld();

...hw is the object instance of the class HelloWorld from the
example_one package....

Still though, the hw object is mostly useless so we are going to now
evoke one of its public methods and display it to the console;

System.out.println(hw.getHelloWorld());

The System class denoted by the capital S is rather powerful and we will
talk about it once again in the future.

So, what's going on here:

I.      The object hw is evoking the public method getHellowWorld.
II.     The public method getHelloWorld is returning the value contained
from with in the string hk.
III.    The sys print line statement is taking this value and displaying
it to the console.

What's a console. In eclipse it's a scrollable edit screen that amongst
other things displays system generated messages such as "Hello World".
You can view the console in Eclipse with alt W + V + C.... It's a bit
sluggish to scroll but I believe it's an aspect of my JFW 7.1.

Closing the console is accomplished with alt dash c....

Here is a fully compiled version of what I've been rambling about for
the last little while:

package main;

/*
 *  entry point of application
 *  written by j. corbett
 *  oct. 31, 2008
 */

Import example_one.HelloWorld;

public class Main {

private HelloWorld hw = null;

        public static void main(String[] args) {

                 hw = new HelloWorld();
System.out.println(hw.getHelloWorld());
        }

} // end of class

Enjoy...

Comments welcomed.

Jim 




James M. Corbett

A / Technical Specialist
GST / HST Micro Development
875 Heron Rd / Ottawa / Ontario

(613) 941-1338



Other related posts: