[thelist] Java

Santilal Parbhu santilal at scorpioneng.co.nz
Mon Jul 28 17:56:13 CDT 2014


Hi Hassan

Thanks for the advice.  I have JDK installed and can run java from the
command prompt.  This might be a bit clumsy to use but I have been able to
try code out.

This is my first time learning OOP.  I have used Fortran in the old days and
I play around with PHP a bit, but I am an amateur and I am not very good at
any language.  I am  learning Java mainly because my daughter is struggling
with it in a first year university course and I thought that I might be able
to help her if I kept a little bit ahead.  But I think I was wrong!

Anyway, I will persevere and maybe once I get my head around how its
structured and what happens at compile time and run time I will be able to
help her out.

Thanks again for taking the time to help.

Santilal


-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Hassan Schroeder
Sent: Tuesday, 29 July 2014 9:39 a.m.
To: thelist at lists.evolt.org
Subject: Re: [thelist] Java

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:

  http://www.beanshell.org/

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;
    //constructor
    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);
10
bsh %

HTH,
--
Hassan Schroeder ------------------------ hassan.schroeder at gmail.com
http://about.me/hassanschroeder
twitter: @hassan
-- 

* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester and archives
of thelist go to: http://lists.evolt.org Workers of the Web, evolt ! 



More information about the thelist mailing list