Tuesday, July 31, 2007

Class Declarations and Modifiers

Most Java programmers think they know all about how modifiers works.
For some of them its true, but for others - its not.
So, just to make sure you are on the right side, i decided to write a short brief about them.

Be aware that this article talk about modifiers of class - not on members!

Lets start with a few simple rules:

  • only one public class per source file.
  • if there is a public class in source file - the name of file must be the same as the class.
  • package statement must be the first line of code.
  • imports statements should be between package statement and class statement.

There are two modifiers types:

  • Access modifiers: public, protected, private.
  • Non-access modifiers ( final,abstract,strictfp).

Access modifiers:

In effect access means visibility.
if class A cant access class B, it means that A cannot see class B.
A cannot use class B in any way.

there are 4 access modifiers:


  1. default
  2. private
  3. protected
  4. public

Default access
Class with default access is a declaration of class without any modifiers.
it means "package level" access.
only classes from same package can access or extend this class.
for example: if class A as default access, then only classes in same package as A, can access class A.

Protected access
Only inner classes can have protected access modifier.
Class with protected access can be access by classes in same package or in a case of inner classes, the protected inner class can be access by extending classes of main class.
for example: if class A has inner protected class B, then if class C extends class A, class C will be able to access class B even if class C is not in the same package as class A and B.

private access
Only inner classes can have private access modifier.
Class with private access can be access only by containing class.
extension classes cannot access this class.

Public access
Class with public access is a class that all classes can access and extends from (unless class is final). no matter if class is in another package or if its a class of a third party. access is allowed.


Non-access modifiers :

class declaration can be changed by non access modifier in addition to access modifiers.
this declarations will result some behavior.

there are a few non-access modifiers for class:

  • final
  • abstract
  • static
  • strictfp

final
when declaring a class as final - it means that this class cannot be extended by anyone. no matter what is the access modifier - this class will never be extended. this restriction could be very useful in case of security reasons and so on.

abstract
when declaring a class as abstract - it means that no one can create an instance of this class, and this class should be extended by other classes. it means that this class is meant to be extend, the opposite of final modifier. abstract class can have abstract methods as well, but for us, is not the issue.

static
when declaring a class as static the class is defined as nested top-level class.

this class is a inner class . A nested top-level class is just like any other top-level class except that it is declared within another class or interface.

strictfp
Marking a class as strictfp means that any method code in the class will
conform to the IEEE 754 standard rules for floating points.

This was a short brief about class modifiers.
I hope i didn't miss anything about it.
I will publish a soon as possible a short brief about members modifiers.

No comments: