Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday 14 July 2017

Java Keywords

Keywords In Java

java keywords


Now, Here in java programming language there are many java keywords are available. But we will learn some of them java keywords with example. So let's starts

(1) Java this Keyword

First, we start with this keyword in java. In java this is a keyword or reference variable which is refers to the current object. In java there are many uses of java this keyword, this keyword can be used to remove ambiguity error from the program, this keyword can be used in constructor chaining i.e calling one constructor from another constructor, etc.

Java this Keyword Example

class Student
{
String s;//data member
int rollno;//data member
Student(String s, int rollno)//constructor
{
this.s = s;
this.rollno = rollno;
}
void get()
{
System.out.println(s+" " +rollno);
}
public static void main(String args[])
{
Student s1 =  new Student(" krishna ", 100);
Student s2 =  new Student(" shankar ", 200);
s1.get();
s2.get();
}
}

output : krishna 100
              shankar 200

In the above example, there data member and arguments passed in constructor is same, so there may be ambiguity error, so remove this error we can use this keyword here.


(2) Java Static Keyword 

Java static keyword basically it is used to save memory. It can be used with variables, methods and nested class. 

We can apply static keyword with variable, method and nested class but in the below example we will use static keyword with method only .

If we declare any method as static than there is no need to create an object of that class because we can call any static method with the class name. If there is no object creation i.e save a memory because object goes into heap memory.

Java Static Keyword Example

class Employee
{
static int id = 50;
static String name = "varun";
static void show()//static method{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Employee.show();//accessing static method with class name
}
}


Some Rules of Static Keyword In Java

  • You can access static method with class name.
  • You cannot access non-static data member in static method.
  • You can change the value of static data member in static method.

 

(3) Java Extends Keyword

Java extends keyword basically used in inheritance concept for re-usability where child class extends(inherit) parent class so that child can use the data member(properties ) and methods of parent class.

Java Extends Keyword Example

class Parent
{
int age = 75;
void show()
{
System.out.println("parent is a teacher");
}
}
class Child extends Parent
{
int salary = 5000;
void get()
{
System.out.println("child is a pilot ");
}
public static void main(String args[])
{
Child c = new Child();
System.out.println(c.age);
c.show();
System.out.println(c.salary);
c.get();
}
}

output : 75
             parent is a teacher
             5000
             child is a pilot

(4) Java Super Keyword 

Java super is a keyword or reference variable which refers immediate parent class object and there are many uses of super keyword in java.

Java Super Keyword Example

class Parent
{
void show()
{
System.out.println("parent");
}
}
class Child extends Parent
{
void show()
{
System.out.println("child");
super.show();
}
public static void main(String args[])
{
Child c = new Child();
c.show();
}
}

output : child
              parent

In the above example, there are parent and child both have the same name method i.e show() method. If you will not use super keyword here, by default child's show method will execute only. If you will use super keyword here then both parent and child's show() method will execute.


(5) Java Implements Keyword

Java implements keyword is used when a class implements an interface in java.


Java Implements Keyword Example

interface Simple
{
void show();
}
class Test implements Simple
{
public void show()
{
System.out.println("hello java ");
}
public static void main(String args[])
{
Simple s = new Test();
s.show();
}
}

output : hello java

(6) Java Final Keyword

Java final is a keyword or non-access modifier which is used to restrict the user. Java final keyword can be used with class, variable, method.


  • If you make any class as a final then you cannot inherit that class.
  • If you make any variable as final then you cannot change the value of this variable.
  • If you make any method as final, you can't override this method in java.



Java Final Keyword Example

If you make any variable as final variable after that you can't change the value of this final variable.

class FinalExample
{
final int a = 45;//final variable 
void show()
{
a = 50;
System.out.println(a);
}
public static void main(String args[])
{
FinalExample f = new FinalExample();
f.show();
}
}

output : compile time error


(7) Java Throw Keyword

Java throw is a keyword in java which mainly used to throw custom exception or user defined exception in java.

Java Throw Keyword Example

class ThrowExample
{
static void test(int age)
{
if(age<18)
{
throw new ArithmeticException("not valid age");
}
else
{
System.out.println("your welcome");
}
}
public static void main(String args[])
{
ThrowExample.test(5);
}
}

output : Exception in thread "main" java.lang.ArithmeticException : not valid age


(8) Java Throws Keyword

Java throws is a keyword in java which is mainly used to declare an exception in java and makes our program handler free.


Java Throws Keyword Example

import java.io.IOException;
class ThrowsExample
{
void a()throws IOException
{
throw new IOException("error");
}
void b()throws IOException
{
a();
}
void c()
{
try
{
b();
}
catch(Exception e)
{
System.out.println("exception is handled");
}
}
public static void main(String args[])
{
ThrowsExample t = new ThrowsExample();
t.c();
}
}

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate