Tuesday, July 19, 2016

What is Keywords in Java?

Java Keywords

Java Keywords

* Keywords are predefined identifiers available directly throughout the JVM.

* They have a special meaning inside java source code and outside of comments and strings.

* For Example : public, static, void

Q1) What are different types of access modifiers in Java?

Ans) There are four different types of modifiers:

Modifier Accessible in the same package Accessible in different package
Private No Yes, only if the class extends the main class
Protected Yes Yes, only if the class extends the main class
Default Yes No
Public Yes Yes

Q2) What is the use of final keyword?

Ans) The final keyword can be assigned to

  1. Class level variable
  2. method
  3. class
  4. Objects

If a final is assigned to a variable, the variable behaves as a constant. It means that the value of variable once set cannot be changed.

final int i=1;
i =5; // error

If a final is assigned to a method then it cannot be overridden in its child class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
      package com.javasunderesan;

class Parent {
 final void print() {
  System.out.println("Inside");
 }
}

class Child extends Parent {
 public final void print() {// error cannot override final method
  System.out.println("Inside");
 }
}

If a class is made as final, then no other class can extend it and make it as parent class. E.g. String Class.

Final objects are instantiated only once. i.e
final Map map = new HashMap();
map.put("key","value");
map = new HashMap();  // error

Q3) What is use of throws keyword?

Ans) The Java throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained. Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
      package com.javasunderesan;

import java.io.IOException;

public class ThrowsKeyword {
 void callTestThrow_1() throws IOException {
  throw new IOException("device error");// checked exception
 }

 void callTestThrow() throws IOException {
  callTestThrow_1();
 }

 void test() {
  try {
   callTestThrow();
  } catch (Exception e) {
   System.out.println("exception handled");
  }
 }

 public static void main(String args[]) {
  ThrowsKeyword obj = new ThrowsKeyword();
  obj.test();
  System.out.println("normal flow...");
 }
}
Output
exception handled
normal flow...

Q4) Why static methods cannot access non static variables or methods?

Ans) A static method cannot access non static variables or methods because static methods doesn't need the object to be accessed. So if a static method has non static variables or non static methods which has instantiated variables they will no be initialized since the object is not created and this could result in an error.


Q5) What is a static method?

Ans)A method defined as static is called static method. A static method can be accessed without creating the objects. Just by using the Class name the method can be accessed. Static method can only access static variables and not local or global non-static variables. For eg:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
      package com.javasunderesan;

public class StaticMethodExample {
 public static void main(String args[]) {
  MainClass.printMe();
 }
}

class MainClass {

 public static void printMe() {
  System.out.println("Welcome to JavaSunderesan.blogspot.in");
 }
}
OutPut:
Hello World
Also static method can call only static methods and not non static methods. But non-static methods can call static mehtods.

No comments :

Post a Comment