Monday, December 5, 2011

Declaration - Part 1

Introduction

Welcome to the first article about OCJP(1Z0-851), the main goal here is help who is planning to get the OCPJ (Oracle Certified Java Programmer), to get a better understanding the articles will be split up into sessions that can have one or more subjects depending on the complexity of that.
To provide these articles the book SCJP Sun Certified Programmer for Java 6 Study Guide is used as reference, however the aim is give an overview of every objective of the exam, furthermore think of a place where you can quickly review what you've studied.
I'd recommend reading the book and following these articles up once the articles are organized at the same way as the book.


Declarations, Initialization and Scoping

"Identifiers & Java Beans" - (Objectives 1.3 and 1.4)

What seems an easy objective of the exam, this objective should be read carefully, once this objective is the first of the book you can forget some rules, also getting a better understanding of this chapter can help you with longer question where the issue is a compiler-error.
Find below the main things that you need to know:

Legal Identifiers

  1. Use only Unicode characters, numbers, currency symbols and connecting characters(like underscores).
  2. Identifiers must start with a letter, a currency character( $ ) or a connecting character such as the underscore ( _ ).
  3. Looking at the second letter of the identifier we still keep the same rule but in addition numbers are allowed, thus a combination of letters, currency characters, connecting characters plus numbers as mentioned are allowed.
  4. Java keywords are forbidden, find a list of Java keyword here.
  5. Finally the identifiers are case-sensitive which means BAR is different from bar.

Find some examples of invalid identifiers above, keep in your mind the invalid identifiers, it's the best way to remind the rules.



Sun's Java Code Conventions

The next subject approached conventions for that there is an article that covers it clearly.

JavaBeans Standars

To achieve a goal you only need to study few basics for the exam.
Basically the variable instances are declared as private and these are used by getters and setters methods, the functionality of these methods are getting information and retrieve successively.
The JavaBeans naming rules are the following:

  1. The prefix of getters methods must be get whether the property is not a boolean for this case you can use either is or get.
  2. The prefix of setter methods must be set.
  3. Getters and setters methods must be marked as public.
  4. To declare a getter and setter method use the prefix( get or set) plus the name of property with the first letter in uppercase.
  5. Setter methods use void which means there is no returning and should have an argument that represents the property type.
  6. Getter methods have a return type which is the same as the property and take no arguments.


Also there are rules for JavaBean Listener, to describes that find above the examples for JavaBean Listener Naming Rules.



"Declare Classes" ( Exam Objective 1.1 )

Source File Declaration Rules

Look at the source below


Find the explanation for each element

  1. Packages - if the class is inside a package you must declare a package statement firstly, otherwise you can ignore it.
  2. Imports - if the class needs to use references of others classes you must declare an import statement, if there is no package statement an import statement must be the first statement of the file, otherwise it would be a second statement.
  3. Comments - Can appear everywhere you want
  4. Classes:
  • The name of public classes must be the same as the name of file.
  • You cannot have more than one public class per file.
  • Imports and packages statements affect all the class in the file.
  • The file can have more than one nonpublic class.
  • If your class doesn't have a public class you can call the file with any name.
A brief explanation of the class used by example is given below.

The first statement is "package com.blogspot.mad4technology" which means the class is inside a package, so this statement must be the first in the file, looking at the second statement we have an import statement, because we need to use a reference for the object "java.util.Date", even more a class "Address" needs the same object and we cannot declare another import statement for this class, the classes in the file share the imports and packages statements.
Talking about the class rules we have two classes into the same file, but notice we've got a public class and nonpublic class what is allowed, if you see the name of file it must be exactly the same as the identifier of the public class in this case "Person".

Class Declarations and Modifiers

Modifiers fall into two categories:
Access Modifiers: Basically this kind of modifier restricts or allows access to a class, if you look at the picture above there are three access modifiers, however there is a fourth access called default or package, when you don't declare an access modifier a class assumes a default access modifier.


Class Access

Default Access: Classes that use this kind of level access have access only within the package.

The second class "Dog" does not compile because the classes are in a different package to solve the problem you need to put these classes in the same package or mark a class Animal as public.

Public Access: Classes that use public modifier are visible everywhere, make sure if you're using the important statement for classes in a different packages.



You noticed you can use only two levels of access because the others don't make sense to use, you'll see it later.


Other nonaccess
: There are others modifiers like final, abstract and strictfp, what you have to know is access modifiers can work with nonaccess modifiers for example public and final, also strictfp and final can be used together but abstract and final cannot.

Final Classes: Using this nonaccess modifier you make a class nonsubclassed which means any class can extend it, for example a class String is a final class and none one can extend it, any attempts to do you'll get a compiler error.


Abstract Classes: The main purpose is the class marked as abstract cannot be instantiated.


Take a look at the code above, the abstract methods don't use { } in their declaration, instead they are using semicolon also you noticed there is a non-abstract method so if you don't mark a method as abstract you have to type its implementation.
If exists at least one abstract method in a class the class must be marked as abstract.

How the post is becoming too long I've decided to split it up with a second article, hope you enjoy it also I'll be waiting for advices to improve the content of blog.


No comments: