[thelist] Java

Hassan Schroeder hassan.schroeder at gmail.com
Mon Jul 28 16:38:54 CDT 2014

On Mon, Jul 28, 2014 at 12:11 PM, Santilal Parbhu
<santilal at scorpioneng.co.nz> wrote:

> My brain is completely tied up in knots.

Learning Java will do that to you. Not to dissuade you, but if this is
your first OO language, you might be better off trying Ruby instead,
at least to start with. Just a thought.

> In the reference above is the following code.
> public class Point {
>     public int x = 0;
>     public int y = 0;
>     //constructor
>     public Point(int a, int b) {
>         x = a;
>         y = b;
>     }
> }
> What does this class actually do?  Is the code complete or is it just an
> excerpt for iluustration?

It's a class, and yes, it's complete. You can compile it, and you
can use it. A Real World(tm) class would probably have a little
more logic built into it, but this isn't just pseudocode.

> The next line is a constructor which creates an instance of the class Point
> with variables a and b.  Then x and y are assigned to a and b.  But why not
> just write public Point (int x, int y).  This does not make sense to me.

Declaring the names and types of variables is required prior to
compiling the class; instantiating an *instance* of a class with
some values is a run-time event.

> Also I thought that you only create an instance of a class when you want to
> use it

True. And the code above is *just a class definition*. It doesn't
instantiate anything; that happens when you do something like

   point = new Point(100,200);

Other than switching to Ruby :-) I'd recommend going here:


and installing BeanShell, so you have a simple interactive space
to play with the language.

so, like:

14:32 ~/projects/testcases/java/oo $ cat Point.java
public class Point {
    public int x = 0;
    public int y = 0;
    public Point(int a, int b) {
        x = a;
        y = b;
14:32 ~/projects/testcases/java/oo $ javac Point.java
14:32 ~/projects/testcases/java/oo $ ls
total 16
drwxr-xr-x  4 hassan  staff  136 Jul 28 13:52 ./
drwxr-xr-x  3 hassan  staff  102 Jul 28 13:51 ../
-rw-r--r--  1 hassan  staff  280 Jul 28 14:32 Point.class
-rw-r--r--  1 hassan  staff  154 Jul 28 13:52 Point.java
14:32 ~/projects/testcases/java/oo $ java bsh.Console

(A separate bsh console window will open up)

bsh % point = new Point(10,20);
bsh % print(point.x);
bsh %

Hassan Schroeder ------------------------ hassan.schroeder at gmail.com
twitter: @hassan

More information about the thelist mailing list