[thelist] Java

Santilal Parbhu santilal at scorpioneng.co.nz
Thu Jul 24 04:08:58 CDT 2014


Hey thanks Lee,

Thanks for such a quick a full answer.  I was a bit worried I might get
lambasted for being such a ninny.  I will digest your answer over the next
day or two.

Thanks again.

Cheers
Santilal

Santilal Parbhu
Scorpion Engineering Limited
PO Box 171
Alexandra
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
moment.


> 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
String.

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).

-- 
Lee
-- 

* * 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