STATIC KEYWORD
There will be times when we will want to define a class member that will be used independently of any object of that class. Normally, a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. We can declare both methods and variables to be static.STATIC VARIABLES :
Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.STATIC METHODS :
Methods declared as static have several characteristics . They can only directly call other static methods. They can only directly access static data.They cannot refer to this or super in any way.SUPER KEYWORD
In java whenever a subclass needs to refer to its immediate super class it can do so by using the super keyword . Super has also two general forms which are briefly discuss below- The first use super is to call super class constructors
class A
{
int
a
A(int
x)
{
a=x ;
}
}
class
B extends A
{
int
b ;
B(int
x, int y)
{
Super(x);
b = y ;
}
}
- The second form of super acts somewhat like this except that it always returns to the super classed sub class in which it is used . This usage as the general form of super member. This member can be either a method or an instance variable the second form is applicable to situations in which members by the same name in the super class.
class A
{
int
i ;
A(int
j)
{
i = j ;
}
}
class B extends A
{
int
i ;
B(int
j , int k)
{
Super i = j ; //initializing ‘i’ of super class.
i = k ; //initializing ‘i’ of the sub class
}
}
Although the instance
variable I , in B hides the i , in A Super allows access to the I , defined in
the super class. Similarly super can be used to call a method of super class
which is hidden by a method with same name in the sub class.
FINAL KEYWORD
Final is a java keyword which is used in three different contest , Firstly in context of variables to declare named constants , in context of methods to stop it from being overriding and thirdly in context of class for preventing it from being inherited. The use of final keyword in each of the three contexts is briefly discussed below:1. USING FINAL TO CREATE CONSTANTS:
A field can be declared as final to ensure that it contains can not be modified. Thus making it essentially a constant . This means that we must initiate a final field when it is declared . It can be achieve in two ways – either by going it a value when it is declared or given it a value within the constructor.For example :
Final int a=10;
Declaring a parameter final prevents it from being changed within the method . Therefore , it is used to create named constants.
2.USING FINAL TO PREVENT OVERRIDING:
To disallow a method from being overridden we can specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden.
class A
{
final
void meth()
{
System.out.println("This is a final
method.");
}
}
class B extends A
{
void
meth() // ERROR! Can't override.
{
System.out.println("Illegal!");
}
}
3.USING FINAL TO PREVENT INHERITANCE:
Sometimes it is required to prevent a class from being inherited. To do this, precede the class declaration with final. Declaring a class as final implicitly declares all of its methods as final, too.
final class A
{
//
...
}
class B extends A //class is
illegal.
{
//
ERROR! Can't subclass A
//
...
}
No comments:
Post a Comment