Access modifiers provide a mechanism for Access Control and different access modifiers can be used to achieve different accessibility. But Non Access Modifiers have nothing to do with accessibility. But it is used to determine specific characteristics of class/variable/methods etc.
Below are the 7 Non Access Modifiers available in Java.
- Final
- Abstract
- Static
- Strictfp
- Native
- Synchronized
- Transient
Final Non Access Modifiers
Keyword
final
Applicability:
- Class
- Method
- Instance Variable
- Local Variable
- Method arguments
Final Class
A class, when set to final, cannot be extended by any other class.
Example: A String Class in java.lang package
Final Method
A method, when set to final, cannot be overridden by any subclass.
Final Variable
When a variable is set to final, its value cannot be changed. The final variables are like constants.
Example : public static final int i = 10;
Abstract Non-Access Modifier
Keyword
- abstract
Applicability
- Class
- Method
Abstract Class
An Abstract class can have abstract methods. If a class has an abstract method, the class becomes an abstract class. A class can also be an abstract class without having any abstract methods in it.
Abstract Method
Abstract methods are those methods that do not have a body but only a signature.
Example : public abstract void method();
Synchronized Non-Access Modifier
keyword
synchronized
Applicability
- Method
Synchronized Method
Synchronized methods can be accessed by only one thread at a time.
Native Non Access Modifier
Applicability
- Method
Native Method
Native methods indicate that the method is implemented on a platform-dependent code.
Strictfp Non-Access Modifier
Applicability
- Class
- Method
Strictfp Class / Method
Strictfp non-access modifier forces floating-point or floating-point operation to adhere to IEEE 754 standard.
Note*: Strictfp non-access modifier cannot be applied on a variable.
Cheatsheet
- Non Access Modifiers available in Java are Static, final, abstract, synchronized & Volatile
- Static keyword can be applied to Variables & Methods.
- Static Variables are those variables that are not associated with any instance but are associated with the class, which means all instances will access the same single copy of the variable.
- Local variables can not be declared as static.
- Static keyword can also be applied to Methods. They will work for all the instances and they will not be dependent on instance creation.
- A final modifier can be applied to methods and variables.
- Final is the only modifier that will be available to the local variable.
- Once declared as the final, the value of the variable can not be changed.
- A final variable doesn’t get default value as opposed to instance variable coz value can’t be changed once assigned.
- A final method cannot be overridden.
Hi.
Can you clear my below doubt?
In the cheat sheet mentioned above there are 2 points which is troubling me.
1. Local variables can not be declared as Static.
2. Final is the only modifier which will be available to the local variable.
But in the previous page regarding static and final explanation it is said that static MUST be used when final keyword is used.
Can anyone tell me which statement is right?
Hi Karthik,
Could you please let me know where it is written that static must be used when final keyword is used.
As for as first 2 points are concerned they are valid in terms of Local Variable. And another statement is about Instance Variable. Though it is not a rule to use static with final instance variable. But it does make sense that final variable should be made static. Coz this variable can not be changed.
Hi Gautham,
Thank you for such a great explanation, It’s really helpful for a person like me who is just started steps in Java World.
Extending to your info, would you be able to provide me the real-time scenarios.
1. When do we need to go for abstract and why?
2. When do we need to go for strictfp, naive and static and why?
Hi Ram,
Abstract classes are used when you want subclasses to follow some rules about the method and also you want to provide the default implementation of some methods. Though in Latest versions of Java, this is not a valid scenario, coz now interface can also have a default method but there are things which can be done with abstract and not interface, So in those cases, it makes sense to use abstract.
I have never used strictfp and naive for myself, so can’t give an example. But the static is a very frequently used keyword. It will be used when you want variable to be class level and not object level. A simple example would be if you want to have an int variable which should be used to count the number of times object of the class has been created. In that case, you will use a static variable to maintain the number.
public class test123 { static int i; test123(){ i++; System.out.println("No of times object created"+i); } public static void main(String[] args) { test123 t1 = new test123(); test123 t2 = new test123(); test123 t3 = new test123(); } }
Hope it help.
Regards
Good web site i am searching for this only .. It’s difficult to find excellent
writing like yours . I truly appreciate individuals like you! Your site content are very easy to understand.
Well explained and understanding post!
All java beginner should go through this concept here, its very useful to know about java non access modifiers in java. Will be looking forward to know about java access modifiers with examples. Thanks for sharing!
good explanations…Thank you gautam
very informative
seriously, y you doesn’t write applicable to interface ?
Hi,
I am new to Java. Could you please provide more programming details on Abstract Class/Method.
I would like to practise more on this.
Thanks!!!
Good job man! Is it possible for you to give some real time example?
Nice website dude. Simple and awesome. One suggestion, think you missed to provide explanation about Transient.
Thanks for your nice comment. There is already an Article on Transient please refer the same.
Regards
You have listed Transient as one non access modified; but not explained it. Pl do the same.
I have already written an article on Transient vs Static. Hope it help you.
http://94.143.139.145/core-java-tutorial/transient-vs-static-variable-java/
Good web site you’ve got here.. It’s difficult to find excellent
writing like yours these days. I truly appreciate individuals like you!
Take care!!
Hi Gautam why don’t u provide navigation….
String belongs to java.lang package …please correct
Hi Geetha,
Thanks for your comment. Article has been updated.
Regards
You have declared a method as abstract method but it doesn’t have the keyword “abstract”. Please update…
could you please explain this statement ?
“A class can also be an abstract class without having any abstract method in it. But once a class have an abstract method class becomes abstract class.”
Hi Rashmi,
There could be 2 different scenarios.
1- A class is having abstract Method
2- A class doesn’t have abstract Method
Scenario 1: If a class is having an Abstract method in that case there is no option but class needs to be declared as Abstract and you have to use abstract keyword with class.
Scenario 2 : If a class doesn’t have any abstract method. In that case there is no reason to use abstract keyword with class. And you are not forced to do that. But you can use abstract keyword with class. So in this case you are free to declare class as abstract or normal class.
Hope now it is clear. In case of any query let me know.
Regards
A class is known as abstract in both the cases with and without abstarat method present in the class
It says tat if in a class any method is abstract,tat class will automatically become abstract class..so we cant make object of the abstract class..so class also become abstract.(pls do refer others.my info. Will not be true)
i couldn’t find explanation for few things in the article such as, 1.difference b/w JDK/JRE/J2EE, 2.Transient, 3.Assignment Statement Topic
your website is pretty awesome. it will still more helpful to new beginner if you explain non-access modifier concept with example for program.
Where the final variables are allocated space in memory ??
stack /heap ????
you have missed the volatile keyword any have nice info…
when –> public final class String…how could we able to change the value?
String x = “abc1”;
x = “543”;
A Class when set to final cannot be extended by any other Class.
No ot will givecompile tym error.because a reference variable I already set .
Nice Information