Member Modifiers
3. final local variables :
i. Local variables won’t get any default values, we should perform initialization before using that variable.
ii. Even though the local variable declared as the final, there is no need to perform initialization until if we r not using the local variable.
Eg : P S V Main( ) P S V M ( )
{ {
final int i; final int i;
} SDP (i);
}
iii. For the local variables, the only allowed modifier is “final”. if v r using any other modifier we will get a compile time error.
Saying : illegal start of expression iv. formal arguments :
The variables which are declared as the arguments of a method are simply act as local variables of that method.
We r allowed to declare a formal parameter as final. If it is declared as the final, then v r not allowed to perform any reassignment within the method.
Eg 1: class test {
public static void M1(int ‘i’ int ‘j’)
{ (I = local variables) (J = formal arguments ) SOP (i); SOP (j);
} P S V M (S[ ] a) { M1 (100, 200); } } Eg 2: class test {
public static void M1(final int i, int j)
{ (formal parameter as final) i = 10;
j = 20;
SOP (i); SOP (j); } P S V M (S[ ] a) { M1 (100, 200); } }
Static :
i. “Static” is a keyword, which can be applied for variables and methods. We can’t apply static keyword for the top level classes but we can apply for inner classes. (Such type of inner classes are called static nested classes).
Necessity of static :
Class student {
String name : → Instance variables Int rollno;
Static String College – name; → static variables }
Conclu :
In the case of instance variables, for every object a separate copy will be created. But in the case of static variables, a single global copy will be creates and shared by all instances.
Eg: class test {
int i = 10; static int j = 20;
P S V M (String[ ] args) {
Test t1 = new test ( ); t1.j = 100; t1.j = 200; Test t2 = new test ( ); t2.j = 300; t2.j = 3000; SOPln (“t1.i + “…….” +t1.j); SOPln (“t2.i + “…….” +t2.j); } }
Disadv of Static :
As all the objects are sharing single global copy of the static variable, if any object changes its value (by using ob; ref), that changed value will be reflected for all the objects. Hence, security is the major problem in the case of static variables.
Hence, there is no thread safety for the static variables. To overcome this problem, usually static variables always associated with “final” keyword.
Eg: class test { int i = 10; P S V M (String args [ ]) { SOPln (i); } }
CTE : non-static variable ‘i’ cannot be referenced from a static context. Static members can be referenced from anywhere i.e. either from static or instance area but non-static members can be accessed only from instance area. Eg : Consider the following 4 declarations :
i) int i = 10; ii) static int i = 10;
iii) public void M1( ) {SOPln (i)};
iv) public static void M1( ) { SOPln (i)};
Which of the following 2 is allowed within a single class : a) i & iii b) i & iv c) ii & iii d) ii & iv
(instance var can be accessed from instance method).
CTE : instance non-static variables, can’t be referenced from static context. (static variables can be referenced from anywhere)
“Static” is nothing but utility and can be used from anywhere by anyone. “Static” means complete implementation available but ‘abstract’ means no implementation available. Hence static, abstract combination is illegal in the case of methods.
Eg : class P > javac p.java {
P S V M(S[ ] a) p.class d.class {
SOPln (“parent main”); > java p
} parent main
} > java c
Class C extends P { }; parent main
⇒ The static methods can be inherited only main method will be executed but any stmtsl…. Present in child class won’t be executed.
We can’t override static methods. It seems to be overriding applicable but it is method hiding ⇒ changing the implementation of a class by using other classes makes no sense.
But overloading applicable for the static methods.
⇒ Inheritance
Overriding (explanation later) Overloading
Transient : (Interviews)
1. “transient” is the keyword which can be applied only for variables. We can’t apply transient for methods and classes.
(Process of saving object (values in object) into a file is known as serialization. If we don’t want to store some of the values of object into a file we go for transient).
2. transient means → not to serialize i.e. while saving the state of object to a file, (this process is called serialization) JVM ignores the values of transient variables. Instead of original value, JVM stores default values.
Eg : class account { string accname; string accno; string UID; string PWD; }
i = 10 i = 10
j = 20 j = 20
Sample S abc.tx+ FOS – File O/P Stream.
OOS – Object O/P stream
(the process of writing an object to a file is called “serialization”)
i = 10 j = 20
abc.tx+ OIS.read Object( ); → return object.
The process of getting (reading) an object from a file is called “De Serialization”.
Demo program w.r.t transient and serialization :
Import java.10.*;
Class transient demo (implements serializable) {
int i = 10; // transient int i = 10, j = 20; int j = 20; // transient int j = 20;
P S V M (String[ ] a) throws exception {
Transient Demo + = new transient demo( ); SOPln (t.i +”…..”+t.j); // 10…..20
File Output Stream FOS = new File Output Steam (“abc.tx+”); Object Output Stream OOOS = new Object Output Stream (FOS); OOS.Write Object (t); /* t1.j=100; t1.j = 100 */
File Input Stream fis = new File Input stream (“abc.txt”); Object input stream ois = new object input stream (fis); Transient Demo t1 = (Transient Demo) ois. read object( );
→
OOS FOS
←
SOPln (t1.i + “…..” +t1.j); // 10…..20
} (same as before)
} // 10……. 0 (j-tray)
// 0……0 (I,j-tray)
‘Static’ variables never participated in the serialization process because these r not part of object state. If we r declaring static variable as the transient there is no effect at all.
All the object can’t be serialized, only objects of class implementing serializable interface can be serialized. Hence we provide implements serializable in the above example.
If we declare a final variable as transient, there is no effect at all.
Before After Serialization Serialization 1. int i = 10; i = 10; int j = 20; j = 20; 2. transient int i = 10; i = 0; int j = 20; j = 20; 3. transient int i = 10; i = 0; transient int j = 20; j = 0; 4. transient static int i = 10; i = 10;
transient int j = 10; j = 0; 5. transient final int i = 10; i = 10;
transient int j = 20; j = 0; 6. transient static int i = 10; i = 10;
transient final int j = 20; j = 20; transient static final int i = 10; i = 10
“Native” Modifier :
1. “native” is the keyword which can be applied only for methods. i.e we r not allowed to use native keyword for the classes and variables.
2. If a method is implemented in non-java (like C, C++ mostly) is called native method (or) foreign method.
In olden days, java is performance very poor when compared to C/C++. But later because of advs of Java slowly shifting to java. But now also performance wise not that much good.
Now – a days 10% real-time projects are with C/C++ with Unix called open systems, - C/C++ code is M/c understandable in a higher level than compared to Java.
Native keyword is breaking Java’s p/f independent concept.
Java code is easily understood by pgmgs when compared to C code but perf wix slow and java pgm should be converted to .class 1st and then to .exe files.
3. The main objective of using native keyword in java is i) to improve performance of the ii) to communicate with language code, (using code developed in any lang. as it is)
4. The use of native keyword breaks p/f I feature of java. 5. Pseudo code for using native libraries in java :
If want to lodd some libraries during the time of loading the class, we put it in static block.
Class native {
static {
System.loadlibrary (“Native Library Path”);// load native libraries. }
native void M1( ); // native method declaration.
} (Implementation is already there) class client
{
P S V M (S[ ] a) {
Native n = new Native ( );
n.M1( ); // invoking a native method. }
} (We can do this in the same native class also)
6. ‘native’ method means – implementation is already available, but abstract method means implementation is not available, child class is responsible for implementation. Hence, native and abstract combination is always illegal for methods.
7. The ‘native’ and ‘strictfp’ combination is also illegal for the methods. - follows IEEE 754 standards but may or may not IEEE 754 stds.
8. We CAN OVERRIDE !!! a native method. It is recommended to override a native hash code method available in the object class.