codescracker


java

Java Non Access Modifiers



« Previous Tutorial Next Tutorial »


There are following non-access modifiers available in Java programming:

Java static Modifier

The static modifier in Java, used to create variable, class, method.

You can use static keyword to define static variables in Java. The static variables will exist independently of any instance created for the class. One copy of a static variable exists regardless of number of stances of the class.

You are free to use static keyword to create static method in Java. Static method will also exist independently of any instance created for the class.

Java static Modifier Example

Here is an example program, illustrates the concept of static modifier in Java:

/* Java Non Access Modifier - Example Program */
		
public class MyTestClass
{
	private static int myNum = 0;
	protected static int getCount()
	{
		return myNum;
	}
	private static void add_Instance()
	{
		myNum++;
	}
	MyTestClass()
	{
		MyTestClass.add_Instance(); 
	}
	public static void main(String[] arguments)
	{   
		int i;
		System.out.println("Starting with " + MyTestClass.getCount() + " instances");  
		for(i=0; i<500; i++)
		{
			new MyTestClass();
		}  
		System.out.println("Created " + MyTestClass.getCount() + " instances");
	}
}

Here is the output produced by the above Java program:

Started with 0 instances
Created 500 instances

Java final Modifier

The final modifier in Java, generally used for finalizing the implementations of the variables, classes, methods.

A final variable in Java, can be explicitly initialized only once.

A final method in Java, can't be overridden by any subclass.

A final class prevent itself from being subclasses.

Java abstract Modifier

The abstract modifier in Java, used to create abstract method, class.

An abstract class in Java, can never be instantiated.

An abstract method in Java, is simply a method declared without any implementation.

Java synchronized Modifier

The synchronized keyword in Java, used to indicate that a method can be accessed through only one thread at a time.

Java volatile Modifier

The volatile keyword in Java, is used to let the JVM know that a thread accessing the variable must ever merge its own private copy of variable with master copy inside the memory.


« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test