Interesting Observations in my Java Certification Journey

Posted on Feb 12th, 2013 by Fernando Zamora

As of today I am an Oracle Certified Associate (OCA). That is because today I passed the OCA Java SE 7 Programmer 1 exam. Overall it’s been a journey that has taken me a little over a year. While I can’t speak on what the exam covers, I can talk about the things I learned about Java while preparing for it. There are many other things that you should know besides what I cover in this brief list, but these are the major interesting points that I learned along the way. They are interesting because they are different than what I am used to in my years of experience with C#.

Access Level Modifiers

Package Private

Let’s start with this one because Access Level Modifiers becaue this concept is essential and without this understanding you will not understand many aspects of Java. You have the Public, protected and private. If you come from a C# background you may think you know these but there are differences. In addition Java has another modifier called the Package Private modifier. The Package Private modifier is the access modifier that is set when no other modifiers have been set (this similar to C#’s internal modifier albeit different significantly). For example

class MyClass{
   MyClass(){

   }
}   

Notice the absence of public on the MyClass declaration. This means that the access for MyClass is Package Private. Also notice that the constructor for MyClass also has Package Private access (due to the absence of a modifier). At the class level this means that the class can be seen only by all other classes within the same package but not by any classes in other packages. At the constructor level it means that the class can be constructed by any other code within the same package. You can think of Package Private as Public but only accessible to the classes within the same package.

protected…but not from classes within the same package

If you come from a C# or C++ background you know that protected means that any method or field within that class that has protected access can be accessed by any subclasses (derived classes) of that class. For example

public class Foo{
    protected void doIt(){};
}

//same package as Foo
public class Bar{
    public void doIt(){
       Foo foo = new foo();

       foo.doIt();  //this is perfectly legal in Java even though it's protected
    }
}

Notice that Bar creates an instance of Foo and calls the protected method doIt of Foo and it’s all perfectly legal.

Exceptions

Java has the concept of checked exceptions. In order to fully understand Java code you must know exceptions and at least memorize the list of the most common checked exceptions and Runtime exceptions. A checked exception must be caught by the caller or specified by the calling method. Runtime exceptions on the other hand don’t have this requirement. Not much to say about that other than that you should have a solid understanding of Checked Exceptions, Runtime Exceptions and Errors.

Use of static modifier

In Java the static modifier is used in a very interesting way. Methods and fields that have the static modifier can be accessed via the instance of the class. For example

public class MyClass{
   public static int age;
}

public class Foo{

    public void Bar(){
        MyClass myClass = new MyClass();

        myClass.age = 21;  //this is perfectly fine

        MyClass.age = 22;  //this is also fine

        out.println(myClass.age);  //This outputs 22 because age is static
    }
}

One gotcha about the static modifier is that when used in methods, the method invoked will be the method that belongs the reference class. For example

public class Foo{
    public static String getIt(){
       return "Foo";
    }
}

public class Bar extends Foo{
    
    public static String getIt(){
       return "Bar";
    }
}

public class Test{
     public void Test(){
         Foo foo = new Bar();

         //prints "Foo" because the reference class is Foo
         //even though the instance is Bar
         out.println(foo.getIt()); 
                                         
     }
}

Classes defined within classes

In Java you can define a class within another class. This can also be done in C# but did you know that you can also define a class within a method in Java? Sounds strange. I thought this type of syntax was unique to JavaScript but I was wrong. Here is an example of perfectly legal syntax in Java.

	public void doBigBang(){
		
                //class defined and nested inside the method
		class Universe{
			
			public int size = 999;
		}
		
		Universe universe = new Universe ();
		
		out.println(universe .size);
	}

That’s about as much as I can write about some of the interesting things that I discovered in Java for now. I have many more to share, so you can expect a part two and possibly three to this series.

Leave a Reply




Post Comment

Connect With Us

Recent Posts

A Guide for Learning Design Patterns

July 28th 2016 by Fernando Zamora If you’ve been writing code for more than a year, you might have h...

Read More

Using UML to Analyze Legacy Code

June 30th 2016 by Fernando Zamora For every programmer out there creating brand new code and working...

Read More

Python vs. Other Languages

April 29th 2016 by Fernando Zamora For the last two months or so my fellow McLane Advanced Technolog...

Read More

Naming Your Adapter When Implementing the Adapter Pattern

October 19th 2015 by Fernando Zamora At some point in your project you may need to use a third party...

Read More

10 Methods to Help You Debug Legacy Code

September 24th 2015 by Fernando Zamora A large majority of the work we do in our project is to fix r...

Read More