Home > Software > Introduction to Java > Modifiers

Satellite Phones - Contents Functioning of a Satellite Phones

4.Modifiers

Modifiers are Java keywords that provide information to compiler about the nature of the code, data and classes.

 

Access Modifiers:

 

Modifiers

Can be used on

Meaning

Private

Can be applied to methods and constructors.

It means only the class can access it, not even sub-classes.  So, it’ll cause access denial to a sub-class’s own variable/method.

Protected

Can be applied to class itself (only to inner classes declared at class level, no such thing as protected or private top level class)

It means all classes in the same package (like default) and sub-classes in any package can access the features. But a subclass in another package can access the protected members in the super-class via only the references of subclass or its subclasses. A subclass in the same package doesn’t have this restriction. This ensures that classes from other packages are accessing only the members that are part of their inheritance hierarchy.

Public

Can be applied to class, Interfaces, and members

It means public class is accessible anywhere, Public interfaces is accessible anywhere and public member is accessible anywhere its class is.

 

 Non-Access modifiers:

 

Modifiers

Can be used on

Meaning

Abstract

Class

Abstract class contains unimplemented methods. And it cannot be instantiated

Method

Abstract method has only signature no implementation. The containing class is abstract

Interface

All interfaces are abstract

Static

Class

Make an inner class top-level class

Method

A static method is invoked through the class name. A static method can access only static variables. Also Static methods may not be overridden to be non-static.

Variables

Static variables are initialized at class load time. A class has only one copy of these variables.

Free floating code-block (static initializer)

Run when the class is loaded, rather than when an instance is created.

Native

Can be applied to methods only. (Static methods also)

It means method is written in a non-Java language, compiled for a single machine target type. Native doesn’t affect access qualifiers. Native methods can be private.

You can pass/return Java objects from native methods

Final

Class

Final classes cannot be sub-classed

Method

Final methods cannot be overridden

Variables

Final variables cannot be changed. (Either a value has to be specified at declaration or an assignment statement can appear only once).

Field

Cannot change its value. Static final fields are compile-time constants.

Synchronized

Can be applied to methods or parts of methods only.

Used to control access to critical code in multi-threaded programs. .For a static method, a lock for the class is acquired before executing the method. For a non-static method, a lock for the specific
object instance is acquired.

Volatile

Can be applied to variables, static variables but NOT final variables

Declaring a variable volatile indicates that it might be modified asynchronously, so that all threads will get the correct value of the variable.

Used in multi-processor environments

Transient

Can be applied to class level variables only.(Local variables cannot be declared transient)

Transient variables may not be final or static.(But compiler allows the declaration, since it doesn’t do any harm. Variables marked transient are never serialized. Static variables are not serialized anyway.) Not stored as part of object’s persistent state, i.e. not written out during serialization. Can be used for security.

 
Satellite Phones - Contents Functioning of a Satellite Phones
     Home                                                Copyright (C) 2003-2012 TutorialsWeb.com                                   Disclaimer                                           Sitemap