Al -Imam Muhammed Ibn Saud Islamic University Visual Programming
Programs using the keyword this, super, throw and and throws in Java Using the Keyword this with variable class OneNumber { int n; void setValue (int n) {this.n=n;}; } public class Thatt { public static void main(String[] ma in(String[] args) { OneNumber x = new OneNumber(); x.setValue(3); System.out.println( x.n ); } }
In the above example, the method setValue tries to set the value of its of its argument to the class variable n. Because the name n is already used in the parameter name, so n=n is absurd. The workaround is to use the “this” keyword to refer to the object. Thus we have this.n=n. Using the Keyword this with object
class C { void setValue (B obj, int h) {obj.n=h;}; }
class B { int n; void setMe (int m) { C h = new C(); h.setValue(this, m); }; } public class A { public static void main(String[] args) { B x = new B(); x.setMe(3); System.out.println( x.n ); } }
In the above example, B has a member variable n. It has a method setMe. This method calls another class method and passing itself as a object.
Al -Imam Muhammed Ibn Saud Islamic University Visual Programming
Programs using the keyword this, super, throw and throws in Java
Using the Keyword super
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass: public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } }
Here is a subclass, called
Subclass,
that overrides printMethod():
public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } }
Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod()inherited from Superclass, Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following: Printed in Superclass. Printed in Subclass
Al -Imam Muhammed Ibn Saud Islamic University Visual Programming
Programs using the keyword this, super, throw and throws in Java
Using throw and throws predefined exception public class Test { public static int divide(int x, int y) throws ArithmeticException { if(y == 0){ throw new ArithmeticException("Cannot divide by 0"); } return x/y; } public static void main(String[] args) { try { System.out.println(divide(4,0)); } catch (Exception exc) { exc.printStackTrace(); //System.out.println(exc.getMessage()); } } }
Al -Imam Muhammed Ibn Saud Islamic University Visual Programming
Programs using the keyword this, super, throw and throws in Java Using throw and throws userdefined exception class MyException extends Exception { public MyException(String msg){ super(msg); } }
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& public class Test3 { static int
divide(int first,int second) throws MyException
{ if(second==0) throw new MyException("can't be divided by zero"); return first/second; } public static void main(String[] args) { try { System.out.println(divide(4,0)); } catch (MyException exc) { exc.printStackTrace(); } } }