[program-java] Java Tutorial 3

  • From: "Corbett, James" <James.Corbett@xxxxxxxxxxxxx>
  • To: <program-java@xxxxxxxxxxxxx>
  • Date: Tue, 25 Nov 2008 14:42:06 -0500

So with the last tutorial put to bed, we need to shift gears and
temporarily leave the discussion of Eclipse behind. Although we are
going to use what we've learned via previous posts we now need to start
playing with Java with in the Eclipse IDE.

Ok, here's what we are going to do next: We are going to create a simple
project based upon the structure we've already created.

1.0     Let's open the Eclipse IDE and select our work space.
2.0     Let's open the project explorer.
3.0     Expand the project "first_time" so that you are with in the src
folder....

You will notice that we have the lower case "m" main package that we had
previously created... remember that this is our package that contains
upper case "M" Main.java which is our interface to the world. If you
adhere to the MVC structure (Model View Controller) as we are in this
project, encapsulation will more or less be assured.

Generally speaking, a package is used to contain related code i.e. .java
files or .property files.. So for this example we will create a new
package titled "example_one", excluding obviously the quotes....
Remember that the package is always lower case....

4.0     Once the package has been created we need to create the
ubiquitous class "HelloWorld".

Upon the creation of the class, you will be placed directly with in an
editor for the same class. Take a moment and scroll up to the top of the
page.... Take note that your new class is part of the package
"example_one"... more about this later.

You should have only really two lines aside from the package line....

4.1     The class declaration line ending in a left brace.
4.2     The class terminator with the single right brace. Just for
laughs and giggles let's add two forward slashes and the text "end of
class".

The double forward slashes denote the beginning of a single line
comment, and the text is just a way of informing you that you are at the
end of your class.

5.0     Move to the top of the page and then to the end of the package
example_one; line.
6.0     Press the enter key twice to give us a blank line and a new line
for editing.
7.0     Type the following: /* and press the enter key again.

Here's what you should see:

/*
*
*/

This is yet another form of commenting in Java. If you enter text on the
line that begins with the single star character and then press the
return key, another star will be created on a new line for editing. Here
is an example of what I do:

/*
*       class for demonstrating java coding techniques.
*       * written by J. Corbett
*       * Nov. 25, 2008
*       */ 

Usually but not always, these comments are followed by your "import"
statements.... More on that later.

What we have now is really just the outline of a class.... Completely
useless to be matter of fact. We really need to add meat to our
sandwich.

8.0     Between the class declaration line and the terminator line we
need to add a constructor method. 
9.0     Type the following "public HelloWorld () {" and then hit the
return key.

You will be placed on a blank line but if you scroll down once you will
notice a corresponding right brace to your constructor method'
declaration left brace.

Generally speaking, your constructor must be public for the rest of your
application to see it, however we will talk later about a private
constructor.

The constructor method must always have the same name as the class so in
this case the class is named HelloWorld and the constructor is also
HelloWorld. Directly following the name is a left and right parentheses.
You can pass arguments between them if necessary. Here is an example of
that:

Public HelloWorld (String myString) { 

}

In this example, I'm passing a parameter of type String and the
parameter is titled myString... but again we will get to that later.

So, we need to discuss "public verses private". Well it is what it
is.... Public can be seen by all and private can't. However it goes much
deeper than that. If you remember that in our Main.class, its
declaration was public so as the world could see it, but if we wanted to
see the HelloWorld.class we would need to create a private instance of
it.... Now hopefully you are wondering how this can be, since HelloWorld
is declared public! 

Here is where the water gets muddy: yes the class HelloWorld is public
to the project but when creating an instance of the HelloWorld object it
becomes private so that it remains hidden to the world.... Are you still
with me?

So, the rule is at least for this example a public class becomes private
when you create the object of that class. The rules of encapsulation are
not broken and Newtonian Physics keeps those dam apples falling on our
heads.

Now, all of these wonderful rules can be and often are broken, hence
leaks, hacking, etc. Poor application design produces poorly functioning
applications.

Now with that entire aside, we still need to talk about public verses
private at the class level.

...as you seen with the constructor example above, it's public....
However with in the class you also can have private members, private and
public methods, all which can maintain the rules of encapsulation eg.

Private String myName = "Jimi";

public void setMyName(String name) {
myName = name;
}

Public String getMyName() {
Return myName;
}

Above are examples of how private and public members and methods
interact with in your public class which can be a private object....
Confused yet....

Here is a fully built class demonstrating what I've been rattling on
about:

package example_one;

/*
*       class hello world
*       * written by j. Corbett
*       * Nov. 25, 2008
*       */

public class HelloWorld {

        private String hw = "Hello World";

        public HelloWorld() {

        }

        public void setHelloWorld(String s) {
                hw = s;
        }

        public String getHelloWorld() {
                return hw;
        }

} // end of class

So, get your head around all of that and by Friday I'll post the second
part of tutorial 3 which will implement the HelloWorld.class and we will
actually use it.

James M. Corbett

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

(613) 941-1338

Other related posts:

  • » [program-java] Java Tutorial 3 - Corbett, James