jump to navigation

Access Modifiers July 27, 2005

Posted by Coolguy in J2SE.


This applies to classes and methods
A class must be declared abstract if any of the following conditions is true :

  • The class has any abstract methods.
  • The class inherits any abstract methods but does not implement them.
  • The class declares that it implements an interface but does not implement all of its methods

This applies to classes, methods and variables.
A variable can be declared as final. Doing so prevents its contents being changed. This means a final variable must be initialized when it is declared. It is common coding convention to choose all uppercase identifiers for ‘final’ variables. Final variables do not occupy memory on a per-instance basis. Thus, it is essentially a constant. If a final variable is a reference to an object, it is the reference that must stay the same, not the object. It means that the reference cannot be assigned to some other object, but data of the object can be changed.

A final class cannot be sub-classed.

A final method cannot be overridden

This can be applied to variables, methods and initializer blocks
When applied to a variable, the variable belongs to the class itself and not to its objects. All the objects of the class share the variable. If you modify the value of a static variable in one object, the value gets changed for all the objects of the class since there is only one variable being shared among all the objects.
A static initializer block is executed when the class is loaded.
Methods declared static have several restrictions:

  • They can only call other static methods.
  • They must only access static data.
  • They cannot refer to ‘this’ or ‘super’ in anyway.
  • A static method cannot be overridden to be non static.

It can be applied to methods only.It indicates that the method body is to be found elsewhere i.e. outside the JVM, in a library. Native code is written in a non java language and compiled for a single target machine type.

It applies only to variables. A transient variable is not stored as part of its object’s persistent state. Many objects, especially those implementing Serializable or Externalizable interfaces, can have their states serialized and written to some destination outside the JVM. This is done by passing the object to the writeObject() method of the ObjectOutputStream class. If the stream is chained to a file output stream, then the object’s state is written to a file. If the stream is chained to a socket’s output stream then the object’s state is written to the network. In both cases, the object can be reconstituted by reading from an object input stream.

There will be times when an object will contain extremely sensitive data. Once an object is written to a destination outside JVM, none of the Java’s elaborate security mechanisms is in effect. If you declare a variable transient, it’s value will not be written out during serialization.

It is applied only to variables. It indicates that such variables might be modified asynchronously, so the compiler takes special precautions. Volatile variables are of interest in multi-processor environments

A class, method or variable declared public can be accessed by any other code in a program.

This member can only be accessed by other members of its class

when a member does not have an explicit access specification, it is visible to subclass as well as to other classes in the same package.

This allows access from everywhere but except for different package non-subclass



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: