Friday, November 6, 2009

SCJP 1.6 Study Guide: Declarations and Access Controls Part 3

In Declarations and Access Controls Part 2, I discussed what it means to be a legal identifier and what is widely accepted in practice. Again, remember that an identifier does not need to conform to conventions to be legal. Take a quick look of the rules here.

Class Declaration and Access Modifiers

In creating a Java application you will mostly create classes and interfaces and how you write them will heavily affect your whole application. In this part I will discuss how we declare classes and how you can modify access to them.

Source File Declaration Rules. Before going to declaring classes let's first take a look at the rules in naming your source files, defining packages and using import statements. In the last section of this series we will go deeper into import statements and packages but if you want to know more about them you can go to Sun's Java Package Tutorial.
  • You can have multiple classes in a source file but only one public class is allowed per source file.
  • If your source file has a public class, the name of the source file should be the same as the public class' name (e.g. a class declared as public class Person should be in a source code file Person.java).
  • A source file can have multiple non-public classes.
  • A source file with no public class can have any filename.
  • If the class is part of a package, the package declaration should be the first line in the source file.
  • If you are using classes from different packages in a source file then import statements for those classes should come next after the package declaration and before the class declaration. If the class is not part of a package the import statements should be the first lines in the source file. If there are no package declaration and import statements, the class declaration should appear on the first line in the source file.
  • Import statements and package declaration in a source file applies to all classes within the source file. You cannot declare different packages for each class in your source file.
  • Lastly, comments may appear anywhere in the source file. They are exempted from the rules mentioned above.
Example:
package com.blogspot.bobhub.scjp;

import java.util.*;
import javax.swing.*;

public class Person{}
class Employee{}
This should be saved in Person.java.

Class Declaration. Nested classes will be discussed in future posts. The following code is a basic class declaration:
class Person{}
Classes are declared using the class keyword followed by the name of the class. The opening brace({) signifies the beginning of the class and the close brace (}) ends the class. The code above compiles and you'll learn that it's the minimum requirement for declaring a class (If you have doubts try it. For a quick help on compiling your .java files go here). In addition to the basic class declaration you can add modifiers. We will discuss class access modifiers first and non-access modifiers next.

Class Access Modifiers. Access modifiers restrict or allow usage of a class. Technically there are 4 access levels but there are only 3 access modifiers (public, private and protected), the last one is called default or package access which you get when you don't specify any access modifier like our first class declaration example. Access modifiers are also used with methods and instance variables.

Although Java has 4 access levels a class can only be declared with a public or default access.

Access control for classes dictates what a class can do with another class. Let's say we have two classes, class X and class Y. When we say that class X has access to class Y it means class X can do one or more of these:
  • Create an instance of class Y.
    class Y{}
    class X{
    public Y y = new Y(); //instance variable
    }
  • Extend class Y.
    class Y{}
    class X extends Y{}
  • Access certain variables and methods within class Y, depending on the access control of those variables and methods.
    class Y{
    public int value; //instance variable
    }
    class X{
    public static void main(String[] args){
    Y y = new Y();
    y.value = 24; //access "value"
    }
    }
Access level is sometimes called visibility. Access modifiers and non-access modifiers are placed anywhere before the class keyword but you will notice that access modifiers usually are placed before non-access modifiers. Suffice to say, a class can only have one access modifier at a time.

Public Access. If a class is declared with a public keyword then all of the classes in any package can access that class. Just don't forget that you still need to import the public class if you're going to use it in a class on a different package.

Example:
package bobhub;
public class Person{}
package bobhub;
public class Employee extends Person{}
Default Access. A class with no access modifier is said to have default or package-level access. This means that only classes within the same package as the declared class has access to it.

Example:
package bobhub;
class Employee{}
package bobhub;
public class TestEmployee{
public static void main(String[] args){
Employee employee = new Employee();
}
}
Class Employee and class TestEmployee will compile. But what if class TestEmployee is declared like this:
package scjp;

import bobhub.Employee;

public class TestEmployee{
public static void main(String[] args){
Employee employee = new Employee();
}
}
Try this on your own and take note of the result.

That ends this post. The next post will be about non-access modifiers we can use with classes. If you have something to share feel free to comment. Discussion is another way to study exams like the SCJP.

0 comments:

Post a Comment