Usage of Java Super Keyword
Usage of Java Super Keyword
https://www.javatpoint.com/super-keyword
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.
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:
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
ReflectiveOperationException
o ClassNotFoundException
o InstantiationException
o IllegalAccessException
o InvocationTargetException
o NoSuchFieldException
o NoSuchMethodException
CloneNotSupportedException
InterruptedException
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
SQLException
a.
o BatchUpdateException
o SQLClientInfoException
o SQLNonTransientException
SQLDataException
SQLFeatureNotSupportedException
SQLIntegrityConstraintViolationException
SQLSyntaxErrorException
a.
o SQLTransientException
SQLTimeoutException
SQLTransactionRollbackException
SQLTransientConnectionException
a.
o SQLRecoverableException
o SQLWarning
ArithmeticException
IndexOutOfBoundsException
a.
o ArrayIndexOutOfBoundsException
o StringIndexOutOfBoundsException
ArrayStoreException
ClassCastException
EnumConstantNotPresentException
IllegalArgumentException
a.
o IllegalThreadStateException
o NumberFormatException
IllegalMonitorStateException
IllegalStateException
NegativeArraySizeException
NullPointerException
SecurityException
TypeNotPresentException
UnsupportedOperationException
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.
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) ;
}
}