Monday 28 April 2014

Java Non Access modifiers



1) Final

-> Final can be applied in class, Methods, instance variable and local variables

-> If class marked as final, we cannot extended. That means we cannot create suib class for that.

-> IF method marked as final, we cannot override the method.

-> If variable marked as final, we cannot change the value.

-> If object reference as final, we cannot asign the new object or any value into the reference.


2) Static

-> Static can be applied for methods and variables.

-> If we are using static for methods or variables, all the objects are sharing the same memory location and refer those values. If we are not using static for methods or variablese, all objects create a new memory location and use those referances.

-> The methods or variables marked as static belongs to the class file memory location.

-> The static memeber should be accessed using the class name.

Ex:

public class ford{
public static void printCarInfo() {
System.out.println("Car Name: Ford);
}
}

Public class fordshowroom{
ford.printcarifo();
}

The above example method printcarinfo() is just print the message. If we are using object for this method, its crreates the seperate memory location for all instance for doing the same process for print those same message. So its waste of the memory location.

But if we are using the static for above method, all the object sharing the same memory location for print the same line of message. Static methods and static variables using .class file memory location. This is one advantage of save the memory allocations.

-> The static variables and methods are called class members. Because both are using the .class file memory location. But non-static variables and non-static methods are called as object members. Because all thos non-static variables referring the respective object memory location.

-> The method marked as static, they can access the other static methods and variables directly inside the same class.

-> Any object instance access the static variables, But the changes will reflect to all the objects.

-> Members marked as static can be final.



3) abstract

-> abstract can be applied for classes and methods only.

-> When a method is marked as abstract- Its should not have any implementation. So its end with ';'. So we have implemenation in sub class. So the abstract method should overriden in subclass or should marked as abstract.

-> when class is marked as abstract- we are not able to create object for those class. But we can create sub class and use those abstract class members.

-> if we have single abstract method in class, we should marked the whole class as abstract.

-> But the class can be marked as abstract even we dont have any abstract method in those class.

-> Abstract class can have concrete class(Non-abstract) methods on it.

-> Abstract methods cannot be marked as final.

-> Abstract class cannot be marked as final.

-> Abstract methods cannot be static.

-> Abstract methods cannot be private.

-> we cannot create object (instance) of abstract class. because abstract class have the incomplete method. its does not have the fully implemented method. so if we call the method by using object, its means incomplete task. so we cannot create instance for abstract class.

4)Strictfp

-> Strictfp is declared for methods and classes. Its have the implementation.

-> when declared inside a class or method, its confirm that those class or methods followed IEEE754 standared while writing code. and which makes methods or class behave in a platform independent way regarding the floating points.

-> strictfp cannot be used withh abstract.


5) native

-> Native modifier can only be applied to methods.

-> native method will always platform dependent code like C.

-> Its doesnot have any implementation. Its end with ;.

-> The native method implementation is omitted.


6) synchronized 

-> Synchronized can only be applied for methods.

-> Synchronized only allow one single thread to execute the code at a time.

-> Synchronized can be used with any access modifier.


7) transient

-> only instance variable marked as transient.

-> The variable marked as transient will not be serialized.


8) Volatile

-> only instance variable marked as volatile.


The following access and non access modifiers applicable for below item

Class - public,default,abstract,strictfp,final

Methods - public,protected,default,private,static,final,strictfp,native,synchronized,abstract

Instance variable - public,protected,default,private,static,final,transient

Local Variable - final

No comments:

Post a Comment