Cody Beth

Greenhorn
+ Follow
since May 20, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Cody Beth

Hi All,
I cleared scjp with 95%.. Missed questions in Threads and Garbage collection.
1) Read K&B book thoroughly.This helped to create a good base of java.. And extermely good tips for the exam. The 2 mock exams in the book are very close to the real exam .
2) And also had done Dan's mock exams. Although I have 3 years of experience but solving the questions cleared lot of java fundamental.Thank you Dan for providing such a good site.
3) Done mock exams from www.javacertificate.com. and www.jdiscuss.com..and whizlabs. These exams are very good.
4) And practised lots of code..

5) And last but not the least kept on reading most of the post in this forum. Many doubts were cleared after reading them. Got most of the good sites from this forum.
Posts by Marlene were always extremely informative .. Thanks Marlene for the same..
Thanks to all the javaranchers, for making this forum extremely helpful ...
And I wish everyone appearing for the exam all the best.
Thanks once again ..
Regards,
Cody
21 years ago
Hi All,
Need to know about Thread.isAlive() method
As we know that we cannot re-start a thread once it has executed a run method, i.e., it is not alive after its dead.
Check the code below..

the output of the code is :
1 = false
2 = true // as start method called
In run method
3 = false // run method is completed
4 = true ??? This is showing the thread is alive but run method did not execute again.

Regards,
Cody
Hi All,
The code will surely give compile time error as method Test() does not exists.
But we can have a method name same as class name.
In the above example, Test() is a constructor, but if we have return type mentioned then this becomes a method and code compiles and runs fine.
Check the code below::

This compiles and the output displayed at run time is :

Having method name same as class name is not recommended but the code compiles and runs.

Thanks,
Cody.
Hi Veena,
Compiler will not display any error even though java.lang.Exception is not thrown by the test() method because of the following reason:
1) java.lang.Exception class is a parent class of unchecked (RuntimeException, ) and checked exceptions. So there are chances that the code written in the try block throws any of the unchecked exceptions.So the compiler will never throw an error.
Note: Even if there is no code in try block and Exception object is caught in the catch block , the compiler will never complain.
But if we catch IOException(checked Exception) , then the compiler gives the error that
"exception java.io.IOException is never thrown in body of corresponding try statement".
This is because IOException is not thrown by any of statements in the try block.

I hope this clarifies your doubt.
Regards,
Cody
Hello,
And for the code

This will print true if the System property named by the argument (which is "true" in the example) exists and is equal to the string "true".
And this type of key does not exists,hence it prints false.

Code can be modified as shown below to achieve the required ouput:

Regards,
Cody
Hi Simon,

In the above example interface y and interface I are member interfaces .
and member interfaces are always implicitly static so they are never considered to be inner classes.
And an inner class can never refer to static members (i.e non-final members)
So inner class m cannot refer to interface I

But in ur second example ..

class m is static nested class that can access static members and hence can access interface I.
for details refer to :
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#270924

Regards,
Cody
21 years ago
Hi friends,
In the code below I removed the sleep() method .. and replaced it with an infinte loop in the run method.. With this Thread object is neither sleeping nor waiting ..
With this the InterruptedException is never thrown ..
And the compiler itself complains if the catch as argument as InterruptedException
The modified code is :

Explanation :In the Java Documentation of Thread its mentioned that InterruptedException will be invoked only when wait(), wait(long), or wait(long, int), join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods are invoked
For details refer to :
http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Thread.html
----------------------------------------------------------------------------
Now taking the actual example as it is ..and calling the t.interrupt() before t.start()
The InterruptedException is never thrown.

I guess the interrupt flag of the Thread object is cleared as t.start() method is called. And hence no InterruptedException thrown.
I am not sure about this.
Regards,
Cody
Hi Thomas,
For the Code following is the explanation :
a) At compile time
1) Object[] array = new String[10];
This is fine as we can assign String to Object.
2) array[0] = new Object();
Compiler checks if array elements are of type Object.
Hence the code compiles.

b) At run-time
as "array" was initialized containing String elements so it expects that each element should always be assigned to String objects only. So the assignment is invalid.
Hence gives java.lang.ArrayStoreException
For more details refer to:
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11430
But if we change the Printable interface to Printable class ..then compile time error of inconvertible types occurs ..
class Test {
static Superclass s = new Superclass();
static Printable p;
public static void main(String[] args) {
p = (Printable) s;
}
}
class Superclass { }
class Printable {
void print() {};
}
So does this mean that Type casting in case of interfaces are checked at run time ..and in case of class , Type casting is checked at compile-time..
Hi Roger,
The flow in amethod() is as follows:
1) FileNotFoundException is raised in the try block,so code in the catch block is executed.
and hence the "No such file found" is printed.
2) And finally block which is part of try-catch-fianlly will be executed just before return statement in the catch block is called.
and hence "Doing finally" is printed.

3) After this execution exits the amethod() (because of return statment in catch block)and -1 is returned to main method.And hence the code after try-catch-finally is not executed

If we comment the return statement from catch block .Then code will execute the //# statements as well.
I hope this clears ur doubt.
21 years ago
Hi ,
With default constructor, the class (say class A)can be instantiated only by any class in the same package, irrespective of the class A modifier (default/public).
If class A is public and it contains a default constructor, then compiler will not allow instantiation of class A outside package with that default constructor.
Although we will be able to access class A outside the package(i.e its method or variables), if we get object of class A.