Sie sind auf Seite 1von 6

Usage of Java super Keyword

1. super can be used to refer immediate parent class instance variable.


2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

https://www.javatpoint.com/super-keyword

super and this keywords in Java


super keyword is used to access methods of the parent class while this is used to
access methods of the current class.

this keyword
1. this is a reserved keyword in java i.e, we can’t use it as an identifier.
2. this is used to refer current-class’s instance as well as static members.

1. super is a reserved keyword in java i.e, we can’t use it as an identifier.


2. super is used to refer super-class’s instance as well as static members.

 Class
 Constructor
 Local inner classes
 Inner class methods
 Instance variables
 Local Variables
 Interfaces

Static variables or methods can be invoked without having an instance of the class.
Only a class is needed to call up a static method or a static variable. If you declare any
variable as static, it is known static variable. The static variable can refer to a common
property of all objects (that is not unique for each object), e.g. company name of
employees, college name of students, etc. Memory in a static variable is reserved only
once in a class area at the time of class loading. One advantage of using static is that it
increases the efficiency of the memory.
Program for Static in Java
Example:

Variables or methods belong to class rather than to any particular instance

 A static method cannot access a non-static variable of a class nor can directly invoke
non-static method
 Static members can be applied without creating or referencing an instance of the class
 A static variable will be shared by all instances of that class which will result in only
one copy

this is another Java keyword which as a reference to the current object within an
instance method or a constructor — the object whose method or constructor is being
called. By the use of this keyword, programmers can refer to any member of the current
object within an instance method or a constructor. This keyword can be used to refer to
the current object, and it always acts as a reference to an object in which method was
invoked. There are the various uses of this keyword in Java. These are:

 For referring current class instance variable, this keyword can be used
 To invoke current class constructor, this() is used
 this can be passed as message argument in a method call
 In a constructor call, this can be passed as argument
 For returning current class instance, this keyword is used

3. List of Common Checked Exceptions in Java


Common checked exceptions defined in the java.lang package:

 ReflectiveOperationException
o ClassNotFoundException
o InstantiationException
o IllegalAccessException
o InvocationTargetException
o NoSuchFieldException
o NoSuchMethodException
 CloneNotSupportedException
 InterruptedException

Common checked exceptions defined in the java.io package:

 IOException
a.
o EOFException
o FileNotFoundException
o InterruptedIOException
o UnsupportedEncodingException
o UTFDataFormatException
o ObjectStreamException
 InvalidClassException
 InvalidObjectException
 NotSerializableException
 StreamCorruptedException
 WriteAbortedException

Common checked exceptions defined in the java.net package (almost are subtypes
of IOException):

 SocketException
a.
o BindException
o ConnectException
 HttpRetryException
 MalformedURLException
 ProtocolException
 UnknownHostException
 UnknownServiceException

Common checked exceptions defined in the java.sql package:

 SQLException
a.
o BatchUpdateException
o SQLClientInfoException
o SQLNonTransientException
 SQLDataException
 SQLFeatureNotSupportedException
 SQLIntegrityConstraintViolationException
 SQLSyntaxErrorException
a.
o SQLTransientException
 SQLTimeoutException
 SQLTransactionRollbackException
 SQLTransientConnectionException
a.
o SQLRecoverableException
o SQLWarning

4. List of Common Unchecked Exceptions in Java


Common unchecked exceptions in the java.lang package:

 ArithmeticException
 IndexOutOfBoundsException
a.
o ArrayIndexOutOfBoundsException
o StringIndexOutOfBoundsException
 ArrayStoreException
 ClassCastException
 EnumConstantNotPresentException
 IllegalArgumentException
a.
o IllegalThreadStateException
o NumberFormatException
 IllegalMonitorStateException
 IllegalStateException
 NegativeArraySizeException
 NullPointerException
 SecurityException
 TypeNotPresentException
 UnsupportedOperationException

Common unchecked exceptions in the java.util package:

 ConcurrentModificationException
 EmptyStackException
 NoSuchElementException
a.
o InputMismatchException
 MissingResourceException

The throw keyword in Java is used to explicitly throw an exception from a method or any
block of code. We can throw either checked or unchecked exception. The throw
keyword is mainly used to throw custom exceptions.

Syntax:

throw Instance
Example:
throw new ArithmeticException("/ by zero");

throws
throws is a keyword in Java which is used in the signature of method to indicate that this
method might throw one of the listed type exceptions. The caller to these methods has
to handle the exception using a try-catch block.

Syntax:
type method_name(parameters) throws exception_list
exception_list is a comma separated list of all the
exceptions which a method might throw.

In a program, if there is a chance of rising an exception then compiler always warn us


about it and compulsorily we should handle that checked exception, Otherwise we will
get compile time error saying unreported exception XXX must be caught or declared
to be thrown. To prevent this compile time error we can handle the exception in two
ways:
1. By using try catch
2. By using throws keyword

Example: To created a User-Defined Exception Class

Step 1) Copy the following code into the editor

class JavaException{
public static void main(String args[]){
try{
throw new MyException(2);
// throw is used to create a new exception and throw it.
}
catch(MyException e){
System.out.println(e) ;
}
}
}
class MyException extends Exception{
int a;
MyException(int b) {
a=b;
}
public String toString(){
return ("Exception Number = "+a) ;
}
}

Das könnte Ihnen auch gefallen