Non-access modifiers provide the JVM with information about a class, method, or variable’s characteristics. In Java, there are seven different types of Non-Access modifiers:
Example:
import java.io.*;
// static variable
class Static_demo {
static String s = "Coderzy";
}
class Demo {
public static void main(String[] args)
{
// No object required
System.out.println(Static_demo.s);
}
}
Output:
Coderzy
Example:
import java.io.*;
class Final_demo{
final void myMethod(){
System.out.println("Coderzy");
}
}
class override_final_demo extends Final_demo{
// trying to override the method available on final_gfg class
void myMethod(){
System.out.println("Overrides Coderzy");
}
}
class Demo{
public static void main(String[] args) {
override_final_demo obj=new override_final_demo();
obj.myMethod();
}
}
Output:
javac /tmp/i3EnX2DS9r/Demo.java
/tmp/i3EnX2DS9r/Demo.java:10: error: myMethod() in override_final_demo cannot override myMethod() in Final_demo
void myMethod(){
^
overridden method is final
1 error
Because we are attempting to override a method that is declared as final, the above code generates an error. We’re attempting to override myMethod() in the override_final_demo class, which is declared as final.
Example:
// abstract class
abstract class abstract_gfg{
abstract void myMethod();
}
//extending abstract class
class MyClass extends abstract_gfg{
// overriding abstract method otherwise
// code will produce error
void myMethod(){
System.out.println("GeeksforGeeks");
}
}
class GFG{
public static void main(String[] args) {
MyClass obj=new MyClass();
obj.myMethod();
}
}
Output:
Coderzy
Example:
import java.io.*;
class Counter{
int count;
void increment(){
count++;
}
}
class Demo{
public static void main(String[] args) throws InterruptedException {
Counter c=new Counter();
// Thread 1
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
for(int i=1;i<=10000;i++){
c.increment();
}
}
});
// Thread 2
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
for(int i=1;i<=10000;i++){
c.increment();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(c.count);
}
}
Output:
20000
. Because both threads are accessing the count value, it’s possible that Thread1 fetches the value and, before Thread2 can increment it, Thread2 reads the value and increments it. As a result, the outcome may be less than 20000. The synchronized keyword is used to solve this problem. If the synchronized keyword is used when declaring the increment() method, a thread must wait for another thread to finish the method’s operation before continuing to work on it.
Example:
import java.io.*;
import java.util.*;
class MyRunnable implements Runnable {
private volatile boolean active;
public void run() {
active = true;int k=0;
while (active) { // line 1
// some code here
k++;
System.out.println(k);
}
}
public void stop() {
active = false; // line 2
}
}
class Demo{
public static void main(String[] args) {
MyRunnable obj = new MyRunnable();
obj.run();
Scanner input = new Scanner(System.in);
input.nextLine();
obj.stop();
}
}
Output:
java -cp /tmp/8E2dLPWDK6 Demo
1
2
3
4
5
6
7
8
9
10
.
.
.
828
829
830
831
Typically, run() is called from one thread (the one from which the Runnable was created), and stop() is called from another thread. If the cached value of active is used in line 1, the loop may not end when active is set to false in line 2. That’s when volatile comes in handy.
Note: also read about the Constructor in Java
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…