C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Final and static keywords in Java with Example

Submitted by Amit Shukla, on June 13, 2017

Learn: What are the final and static keywords in Java, what are their properties, how and when they used with examples?

1) Java - final keyword

Final keyword defines itself that once final keyword is used then one cannot extend or change its value. In java the final keyword is used in different methods to define any variable that can be only assigned one time in program.

Use of final keyword

The final keyword has mainly three uses one of them is to create final class. Second one is to use final methods and third one is to use final data member.

Following are uses final keyword:

  1. Stop Inheritance.
  2. Stop Method overriding.
  3. Stop value change.

final class

It is used to avoid inheritance. Once a final word assigned before class then this class cannot be inherited further. In other words final class cannot have its derived class.

Syntax:

final class <classname>
{
        //define class
}

Incorrect method:

final class X
{

}
final class Y extends X //error: cannot inherit from final X
{

}

final Method

It is used to avoid method overriding in java.

In other words if final keyword is assigned before any function then one cannot use the name of function to create another function.

Syntax:

class classname
{
	final void functionname()
	{
	}
}

Incorrect method:

class X
{
	final void get()
	{
	}
}
class Y extends X
{
	void get() //error: get() in Y cannot override get() in X
	{
	}
}

final data member:

It is used to define constant identifier.

If final keyword is used before any variable then one cannot change its value. In other words once final keyword is used then one cannot overwrite or change its value.

Syntax:

class classname
{
	void functionname()
	{
		final int x=100;
	}
}

Incorrect method:

class classname
{
	void functionname()
	{
		final int x=100;
		x=x+10; //error: cannot assign a value to final variable x
	}
}

2) Java - static keyword

static keyword is used java for memory management. Static variables are usually stored in static memory. Static variables are rarely used other than being declared as constants.

We can use static keyword with methods, variable, blocks and nested class. The static keyword belongs to the class than instance of the class.

static can be:

  1. Method
  2. Variable
  3. Block
  4. Nested class

a) static Variable

static variables are formed by declaring static keyword before variable. The static key word is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.

static variables are also known as class variables. Local variables cannot be declared static.

Properties

  1. It makes programs memory efficient.
  2. The static variable can be used to refer the common property of all objects.
  3. The static variable gets memory only once in class area at the time of class loading.

Consider the Example

class Count
{
	static int c=0;
	Count()
	{
		c++;
		System.out.println(c);
	}
	public static void main(String arg[])
	{
		Count c1=new Count();
		Count c2=new Count();
		Count c3=new Count();
	}

}

Output

1
2
3

b) static Method

Static methods are formed by declaring static keyword before any method. Static methods take the data from the parameters and compute the result from those parameters with no reference variables.

Properties:

  1. Any static method belongs to class instead of object of a class.
  2. It can be invoked without using the need of creating an instance of class.
  3. These methods can access static data member.
  4. These methods are able to modify static data members.

Consider the Example

class Company
{
	int id;
	String name;
	static long salary;

	static void change()
	{
		salary=salary + 50000;
	}
	Company(int i,String n,long s)
	{
		id=i;
		name=n;
		salary=s;
	}
	void show()
	{
		System.out.println("Id of employee is "+id);
		System.out.println("Name of Employee is "+name);
		System.out.println("Salary of Employee is "+salary);
	}
	public static void main(String arg[])
	{
		Company.change();
		Company c1=new Company(100,"Shivangi",100000);
		Company c2=new Company(101,"Prerana",200000);
		Company c3=new Company(102,"Karishma",150000);

		c1.show();
		c2.show();
		c3.show();
	}
}

Output

Id of employee is 100
Name of Employee is Shivangi
Salary of Employee is 150000
Id of employee is 101
Name of Employee is Prerana
Salary of Employee is 150000
Id of employee is 102
Name of Employee is Karishma
Salary of Employee is 150000








COMMENTS