[thelist] Java

Santilal Parbhu santilal at scorpioneng.co.nz
Mon Jul 28 14:11:15 CDT 2014


I have spent a few days trying to understand Lee's comments but I seem to be
going backwards.  I have looked up the recommended resources and I am
currently looking at

My brain is completely tied up in knots.  I think I will need to break it
all down into understandable chunks (if that exists for me).  

In the reference above is the following code.

public class Point {
    public int x = 0;
    public int y = 0;
    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?
The first two lines declare and initialise the variables x and y to 0.
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.
Also I thought that you only create an instance of a class when you want to
use it like in the next example on the link:

public class Rectangle {
    public int width = 0;
    public int height = 0;
    public Point origin;

    // four constructors
    public Rectangle() {
        origin = new Point(0, 0);
    public Rectangle(Point p) {
        origin = p;
    public Rectangle(int w, int h) {
        origin = new Point(0, 0);
        width = w;
        height = h;
    public Rectangle(Point p, int w, int h) {
        origin = p;
        width = w;
        height = h;

    // a method for moving the rectangle
    public void move(int x, int y) {
        origin.x = x;
        origin.y = y;

    // a method for computing the area of the rectangle
    public int getArea() {
        return width * height;

Line 1 declares the class Rectangle.  Line 4 declares an instance of the
class Rectangle and it is named origin. 

I think 

public Rectangle() {
        origin = new Point(0, 0); means create a new instance of Point, call
it origin and use 0,0 as the values (but for what??)  Also what is the
reason for the statement public Rectangle().  What is the relationship
between these two classes Rectangle and Point.

Each constructor creates an instance of the class, each with the same name
origin, but each is different because of its signature.  But I can't
understand what    

public Rectangle(Point p) {
        origin = p;

      origin = new Point(0, 0);
        width = w;
        height = h;

    public Rectangle(Point p, int w, int h) {
        origin = p;
        width = w;
        height = h;

are trying to do?

I am sorry to be asking such messy questions, but I am struggle to describe
what I am failing to understand.  Thanks to anyone who has the time to help


Santilal Parbhu
Scorpion Engineering Limited
PO Box 171
Phone: +64 3 440 2100
Mobile: +64 21 2655991
Email: santilal at scorpioneng.co.nz
Web: www.scorpioneng.co.nz

-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Lee Kowalkowski
Sent: Thursday, 24 July 2014 8:57 p.m.
To: thelist at lists.evolt.org
Subject: Re: [thelist] Java

On 24 July 2014 06:21, Santilal Parbhu <santilal at scorpioneng.co.nz> wrote:

> Hi
> I am trying to learn Java and I am hoping that someone can help me get 
> my head around it.  Below is the Hello World example.  I am finding it 
> very hard to understand despite two days research on the internet.
This is no surprise, I have been working as a Java Developer for 12 years.
 I think it's the only language I've ever used that absolutely needs a
feature-rich IDE, and that says a lot for the typical (over) complexity of
the programs you will be working on.

> /** Comment
>  * Displays "Hello World!" to the standard output.
>  */
> public class HelloWorld {
>       String output = "";
>       static HelloWorld helloObj;  //Line 1
>       public HelloWorld(){
>             output = "Hello World";
>       }
>       public String printMessage(){
>             return output;
>       }
>       public static void main (String args[]) {
>             helloObj = new HelloWorld();  //Line 2
>             System.out.println(helloObj.printMessage());
>   }
> }
> The first line creates a new class - correct??

Um, well, ha, it declares a class, let's not use the word create at the

> Then the variable "Output" is initialised as a string.

Hee hee, that's how it looks, but it's not a static variable, so it's not
initialised until you create an object of this class.

> I think static HelloWorld helloObj creates a new class within a class.  
> Why would you do this?

Well, since output is an instance variable, you need an instance of this
class in order to use it.

> public HelloWorld(){ - I have no idea what this does??

That, is a constructor, this contains initialisation instructions that are
executed when an instance of this class is created.

> Then it looks like output is updated to contain "Hello World".  Why 
> not just initialise this to "Hello World" above??

Indeed! lol I don't know why output is initialised to an empty string (I
think it may be more correct to say 'the empty string', but whatever), that
is certainly redundant.

> I think the next bit calls the printMessage() method, but I don't know why
> it is preceded with "Public String".

It declares a method  called printMessage (in Java, we call functions
methods), the method is public, so any class may create an instance of
HelloWorld and call printMessage.  String declares the return type, this
means the method has a bad name, because it prints nothing, it returns a

In Java, return types must be declared, and the compiler will check to see
if the method has a reachable return statement, and that all return
statements return the declared type.

If printMessage() really did print the message, it wouldn't need to return
anything.  If printMessage() did not return any value, then the declaration
would be 'public void printMessage()' (you still have to declare the return
type, unless it is a constructor method, constructors don't return
anything, that's some magic done by the 'new' operator).

> I think the "return output" somehow tells the method to print the contents
> of output.

Yee...No, it's just returning a String, hey, it should be called
getMessage, that's the Java style, which also flies in the face of
encapsulation, a major Object Oriented principle, but you will find get
methods are abundant in Java (and set methods, we call them setters and
getters, I like dogs, I personally championed setters and retrievers, but
that never caught on), abolishing setters and getters would simplify your
code no end, so nobody does that.

> Is the last bit the main method of the class.

Yes, if you're familiar with the C world, you'll be at home with this
method.  This is the entry point into the Java application.  If you're
writing Java Web applications (in a WAR - dum der - Uh!  What is it good
for? - Edwin Starr, 1970) or Enterprise applications (in an EAR, which you
need if using EJBs *cringe*), forget this method lol, you will rarely use
it.  Your code will run in a container (called a server), there will be a
main method somewhere in the container's code (JBoss, Tomcat, Weblogic,
WebSphere, Jetty, Glassfish).  You will somehow point your server to your
WAR or EAR file and it will load your application, so you can debug, I mean
run it.

> But I don't understand the
> construction at all.  What is the helloObj = new HelloWorld do.

Yay, that instantiates an instance of your HelloWorld class (yippee), which
sets up your output 'member' variable (don't snigger at member, the novelty
will wear off).

> That last line prints the output, but I thought that printMessagge above
> did
> this.

Nope, System.out.println prints the output. See?  The code has already been
made too complicated to follow, simply by carelessly naming methods and
variables.  I think naming things well must be the hardest skill judging
from all the code I've seen in my time, in any language.

> Can anyone point me to a good resource to help me anwser these questions?

Ooops, should have read this bit first I guess...

Go to http://docs.oracle.com/javase/tutorial/ and skip the 'What's New'
section (because everything is ha ha).  The next section is "Trails
Covering the Basics" (see what I mean about naming things well?).  Start
there, their Hello World example is far far far simpler, even though they
do have a Hello World tutorial each for NetBeans, Windows, and Linux.

The best way to learn anything like this is to get yourself started and
have a friend (or in a work environment, a 'buddy') that knows it well and
can tolerate you pestering them every half hour.

Oh oh oh, don't forget JavaRanch, that was pretty useful years ago:
http://www.coderanch.com/how-to/java/JavaBeginnersFaq not been there in a
while, and whenever I stumble on a potential answer there, I have to check
it's up to date since it's been around for a very long time (stackoverflow
is waay easier).


* * Please support the community that supports you.  * *

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