Murgan Sub

Greenhorn
+ Follow
since Feb 06, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Murgan Sub

Have a look at the code below
public class DivisionByZero3 {
public void division() {
int num1 = 10;
int num2 = 0;
try {
int t= num1/num2 ;
return;
}
catch (ArithmeticException e) { // (2)
System.out.println("Dealt with " + e);
System.out.println("catch.");
System.exit(1);

}
finally { // (3)
System.out.println("Finally done.");
}
System.out.println("Returning from division."); // (4)
}
public static void main(String args[]) {
new DivisionByZero3().division();
System.out.println("Returning from main.");
}
}
Here the finally block does not execute.
Is there any other way to ensure the finally
block does not execute ..
public class Foo {
int i=5;
public void aMethod() {
System.out.println(this.i);
Object o = new Object(){
int i = 7;
public void aMethod(){
System.out.println(this.i); System.out.println(Foo.this.i);
}
}; // Semicolon required here
}
}
Just Corrected the syntax
Have a look at this code below..
public class TestThread extends Thread{
private String mesg="";
public void run(){

while(!isInterrupted()) {
try {
sleep(1000);
}
catch(InterruptedException e){
}
System.out.println(mesg);
}

}
public TestThread(String m) {
mesg = m;
setName(m);
}

public static void main(String[] args){


TestThread t1=new TestThread("good");

TestThread t2=new TestThread("goodhey");

TestThread t3=new TestThread("goodheyhey");


t1.start(); t1.interrupt();
t2.start(); t2.interrupt();
t3.start(); t3.interrupt();
System.out.println("Hey");
}
Which is better for a check in "while"
isInterrupted or interrupted()
Why are return statements not allowed in
static initializer or an instance initializer?
Kindly Explain..
What happens when two constructors call each other?
Can you get an infinite loop out of the process?

public class ConsRecurs {
public ConsRecurs(int i) {
this(i*1.0);
}
public ConsRecurs(double d) {
this((int)d);
}
public static void main(String[] a) {
System.out.println("About to construct...");
new ConsRecurs(Math.PI);
System.out.println("Hey, I'm still alive!");
}
}
Can you call a static method of an abstract class?
Please Explain..
Kindly look at the code below..
public abstract class AbstractStatic {
public static void main(String[] argv) {

System.out.println("Hello. The answer is yes.");

AbstractStatic.foo();

}
public static void foo() {
System.out.println("Hello from foo. The answer is still yes.");
}

}
Have a look at the code given below
public class TestLock {
public static void main(String[] args) {
TestLock t = new TestLock();
synchronized(t) {
synchronized(t) {
synchronized(t) {
System.out.println("made it!");
}
}
}
}
}
Kindly Explain ..
The following statement results in a compile-time error:
while (false) { x=10; }
While this
if (false) { x=3; }
does not result in a compile-time error
Kindly Explain?
Have a look at this code below
public class ExampleInc {
int i = 0;
ExampleInc increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
public static void main(String[] args) {
ExampleInc x = new ExampleInc();
x.increment().increment().increment().print();
}
}

Multiple operations can easily be performed on the same object, since increment() returns the reference to the current object via the this keyword ?
Look at the following code below ..
class ExceptionOne extends Exception {
public String toString() {
return "Exception one!";
}
}
class ExceptionTwo extends Exception {
public String toString() {
return "Exception Two";
}
}
public class LostException {
void f() throws ExceptionOne {
throw new ExceptionOne();
}
void dispose() throws ExceptionTwo {
throw new ExceptionTwo();
}
public static void main(String[] args)
throws Exception {
LostException le = new LostException();
try {
le.f();
} finally {
le.dispose();
}
}
}
The output :
Exception in thread "main" Exception Two
at LostException.dispose(LostException.java:18)
at LostException.main(LostException.java:26)
Why is there no evidence of ExceptionOne ?
M and N are interfaces.
Look at this code below.
This is a way of implementing an interface
using an Inner Class
interface Inter {
int value();
}

public class Example {
public Inter cont() {
return new Inter() {
private int i=12;
public int value() { return i; }
};
}
public static void main(String[] args) {
Example p = new Example();
Inter c = p.cont();
}
}
Have a look at this code below..
class A {}
abstract class B {}
class Z extends A {
B makeB() { return new B() {}; }
}
public class MultiImplement {
static void takesA(A a) {}
static void takesB(B b) {}
public static void main(String[] args) {
Z z = new Z();
takesA(z);
takesB(z.makeB());
}
}

With concrete or abstract classes, inner
classes are the only way to produce the effect
of "multiple implementation inheritance ?
Have a look at this code below
Apparently there are two ways of implementing
multiple interfaces.
One with a single class other with inner class.
interface M {}
interface N {}
class A implements M, N {}
class B implements M {
N makeN() {
// Anonymous inner class:
return new N() {};
}
}
public class MultiInter {
static void takesM(M m) {}
static void takesN(N n) {}
public static void main(String[] args) {
A x = new A();
B y = new B();
takesM(x);
takesM(y);
takesN(x);
takesN(y.makeN());
}
}
What are the advantages of using an inner class
over a single class ?
Kindly see this code below

Which allows you to put code in the interface ...
What is the advantage of having a static Inner
class in an interface ?
PS:
Please use code tags when ever posting source code. Thanks.
- satya
[ February 12, 2002: Message edited by: Madhav Lakkapragada ]
What are the advantages of nesting interfaces?