First Steps to Apex Hero-dom (or decoding a Java class)

HeadStartJavaLast night I took my first steps on the path to becoming a code hero. I started with a book, Head First Java, recommended by one of my code heroes, David Liu from www.sfdc99.com.

So far Head First Java is an easy read. It’s got an interesting method of teaching, with lots of visuals and a conversational style. It also has a bunch of puzzles (yes, real puzzles like crosswords, fill in the blanks, match ups, etc.) that appeal to my learning style.  But if my goal is to learn Apex, why am I reading a book about Java? Because Apex was based on Java and a lot of the syntax is (apparently) very similar to Apex, and this is a great book (so far) to help learn Java.

It helps me remember if I write things down, so bare with me while I explain what I’ve just learned.

  1. Java can be written in any text editor (I’m probably going to use Sublime Text, but any text editor will do).
  2. It’s written in source files, which contain a single class
  3. Classes contain methods, which in turn contain statements which are what do things

For example, if I were to write a class for a dog, then there may be many methods, like bark(), walk() and eat(). Each of these methods will contain the code that actually makes the method work.

The basic structure of a class is as follows:

public class MyFirstApp 
     { public static void main (String[] args) 
         {
          system.out.print("Java is cool, ");
          system.out.print("and so is Apex!");
          }
     }

Breaking down the code, step by step:

  • public – means that anyone can access this class
  • class – says we’re defining a class
  • MyFirstApp – is the name of the class
  • Everything between the orange curly braces is the contents of the class, in this case there is just one method, the contents of this are contained between the blue curly braces
  • public – means that anyone can access this class
  • static – no idea yet, but I’ll let you know when I find out!
  • void – means that this method doesn’t return anything
  • main – the name of the method, and also the starting point for the class. Every class needs one main method. It’s the first method to be run in the class, the “start here” of the class
  • (String[] args) – methods are sometimes be fed information, or “arguments”. In this case we’re feeding it an array (think group of data) of strings.
  • system.out.print –  methods contain statements and this says we want to print out something, in this case “Java is cool, and so is Apex!” (without the quotes).
  • ; – all statements end in a semicolon

There was more that I learned, but this is probably enough for now. but more to come.

I’m stoked that I understand the stuff that I read about. It’s crazy, because two weeks ago I had no clue what I was looking at. That’s great news, because I also saw some stuff I didn’t understand, and I know that if I stick with it, I eventually will. That’s my story and I’m sticking to it!