CHAPTER-2(Classes and Objects)                                   BOTTOM


Keywords in Java:-

Keywords are reserved identifiers that are predefined in the language and cannot be used to denote other entities. All the keywords are in lowercase, and incorrect usage results in compilation errors.All these reserved words cannot be used as identifiers.

Important keywords related to class and variables:-

Other modifiers for Members:-


Features of static members:-

1. Static members are different from instance members.
2. Static members are part of the class and not object.
3. Static members can be accessed through class name as well as through object reference.
4. Static members can only call static members and not instance members.
5. Static variables are initialized when the class is loaded as opposed to instance  members which          are initialized when the class is instantiated.
6. Static variables act on behalf of the entire class.
7.Static blocks can only call other static methods.
8.Static blocks must only access static data
9.Static blocks cannot refer to this or super in any way.

If you need to do computation in order to initialize your static variables, you can
declare a static block which gets executed exactly once, when the class is first loaded.
The following example shows a class that has a static method, some static variables,
and a static initialization block:

// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
static {
System.out.println("Static block initialized.");
b = a * 4;
public static void main(String args[]) {

As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.

Final :-

Final keyword is used in three context

1. Final with variables.
2.Final with methods.
3.Final with classes.


Use of  Final with variables:-(PROVIDES CONSTANTS)


The   final keyword  when used with the variable makes the value of the variable Constant. It means that the value of the variable cannot be changed once it has been initialized. This applies to both instance as well as static variables.

When used  with object reference variable the value of the reference variable will also be not changed  throughout the programme once the object is instantiated as a final variable.

Advantages of final keyword used with variables.


1. It  is commonly used to define named constants.
2. Readability of the program increases because we can used name of the variable in our class instead of values

   final float PI=3.14;

Instead using 3.14 in our programme we can use PI ,so that  it is easy to remember and the readability of the programme increases.


Use  of  final with classes:-(PREVENTS INHERITANCE)

When we use final keyword with a class then that class is considered complete and need not to be further specialized. So it means that a class declared as final cannot be inherited  by any other class. String class defined in java.lang package is an example of final class.If we try to extend the final class it will throw a compile time error.


final class A {
//body of class A .
// The following class is illegal.
class B extends A {      // ERROR! Can't subclass A
//body of class B


Use of final keyword with methods:-(PREVENTS OVERRIDING)

When the final keyword is used with methods then the behavior of the method cannot be changed in the subclasses .It means that a method declared as final  in the super class cannot be overridden with the same name and signature.

class Light {
// Final static variable (1)
final public static double KWH_PRICE = 3.25;
int noOfWatts;
// Final instance method (2)
final public void setWatts(int watt) {
noOfWatts = watt;
public void setKWH() {
// KWH_PRICE = 4.10; // (3) Not OK. Cannot be changed.
class TubeLight extends Light {
// Final method in superclass cannot be overridden.
// This method will not compile.

public void setWatts(int watt) { // (4) Attempt to override.
noOfWatts = 2*watt;

public class Warehouse {
public static void main(String[] args) {
final Light aLight = new Light();// (5) Final local variable.
aLight.noOfWatts = 100; // (6) OK. Changing object state.
// aLight = new Light(); // (7) Not OK. Changing final reference.

Abstact :-

 Abstract keyword is used with methods and classes.Abstract methods do not have implementation. They tell us what should be done but not how it should be done. It is the responsibility of the subclasses to provide the implementation of the abstract methods.

A class which has an abstact method is an incomplete class because it does not have the implementation of atleast one abstract method and hence is also declared as an abstract.The class which extends the abstract class must provide the implementation of the abstract method defined in the super abstract class otherwise it will also be declared as abstract.


abstract Methods and classes

Abstract method does not have an implementation; that is, no method body is defined for an abstract method, only the method prototype is provided in the class definition. Its class is then abstract (i.e.incomplete) and must be explicitly declared as such . Subclasses of an abstract class must then provide the method implementation; otherwise, they are also abstract.Following example illustrates the use of abstract methods.
Only an instance method can be declared abstract. Since static methods cannot be overridden, declaring an abstract static method would make no sense. A final method cannot be abstract (i.e., cannot be incomplete) and vice versa. The keyword abstract cannot be combined with any nonaccessibility modifiers for methods. Methods specified in an interface are implicitly abstract, as only the method prototypes are defined in an interface. In Abstract class we can define the implementation of some methods whereas in the interface all the methods are implicitely abstract.

abstract class Light {
// Fields
int noOfWatts; // wattage
boolean indicator; // on or off
String location; // placement
// Instance methods
public void switchOn() { indicator = true; }
public void switchOff() { indicator = false; }
public boolean isOn() { return indicator; }
// Abstract Instance Method
abstract public double kwhPrice(); // (1) No method body


Note :
After Successful completion of Training Candidate will be provided with Project Report and Training Certificate.

Home  |   FeedBack  |   Terms of Use  |   Contact Us  |   Report Error
                                                                            Copyright 2009 R.M Infotech (P) Ltd.                                             Designed by: Raman