Vishram Khare

Greenhorn
+ Follow
since Sep 23, 2007
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 Vishram Khare

Hello everybody,

Suppose I define a method local inner class inside a method doStuff as--

public void doStuff(){
int local;

class MyInner{
System.out.println(local); //will not compile
}
}

This code will not compile because it is trying to access the local variable defined inside the method.The reason is that there could be objects of MyInner class created inside the doStuff() method and those local variables will go out of stack once the method doStuff() finishes execution.So the objects of MyInner class trying to access the local variables of the method does not make sense.

However why can the objects of MyInner class access the local variables inside doStuff() method if the local variables are declared final?Anyways final local variable would simply mean that its value cannot be changed.However it will go out of stack once the method finishes execution.In that case,how is it different from a non-final local variable?Anyways final local variable cannot be accessed from outside the method it is declared.So how can an instance of method local inner class (MyInner) access the final local variable?

Could anybody please let me know the same?

Thanks
16 years ago
On page 527 of book--Beginning Java 2 JDK 5 edition by Ivor Horton, it is written that the class,the object of which you want to serialize has to fulfill the following conditions---

i) The class must be declared public.
ii) The class must implement interface Serializable.
iii)If the class has a direct or indirect superclass that is not serializable, then that class must have a default constructor.The derived class must take care of transferring the base class data members to the stream.

2nd and 3rd conditions are obvious and can be understood.

However is 1st condition necessary?Because I wrote a code that had a serializable class with default access in package (not public) and still the program compiled and ran well.

Further on page 528 of the same book,the author says if the class is not public,InvalidCastException might be thrown. In my code,the program did not throw any exception even though the class was not public.

Does anybody have any idea about it?

Thanks

----Vishram
16 years ago
Thanks Dhwani,Prateek,Gurpreet and Ahmed.Thanks Ahmed for giving the link to other discussion on the same topic.Jesper Young's reply there solves all the doubt.

Thanks again.

----Vishram
Hello everybody,

Consider the following code-- (Taken from javabeat mock exam)

public class Test3{

public static void main(String args[]){
System.out.println(method());
}

public static int method(){
try{
throw new Exception();
}
catch(Exception e){
throw new Exception();
}
finally{
return 3;
}
}
}

In this case I am throwing a new exception in catch block but not catching it nor I am declaring that the method throws any exception.Java requires all checked exceptions be properly handled or declared.Otherwise compiler complains.

However in this case,the program compiles and runs successfully.It gives output 3.

But if I comment out finally block, compiler complains that the exception must be handled or declared.

That means finally block is causing the program to compile even if it is doing an illegal thing (throwing an exception that is not handled or declared). Is it not contrary to what we expect? (Compiler should never allow any illegal thing no matter what)?How does finally block make a difference?

Your response will be greatly appreciated.

Thanks

---Vishram
>>it looks like the static block is executed only once

Yes the static initialization block is executed only once no matter how many objects of the class you have.

Suppose you have a class type Test and you want to instantiate an object with the name 'myTest' using constructor myTest1=new Test(); In that case, overall process is as follows--

1) First the class file is loaded into JVM and right that time static block runs. Remember static block will run only once.

2) First a constructor of the superclass of the Test class is called and variables are initialized according to the code in the superclass constructor.In the superclass constructor, first thing done is invoking its superclass constructor and this process goes on and on till class Object's constructor is invoked.

3) Then code returns to the constructor of Test class.Before any other code in the test class constructor is invoked,the non-static initialization bloak in the code of Static class is invoked.

4) Suppose you want to create another object of class Test and you write Test myClass2=new Test(); In this case, steps 2 and 3 are run but not step #1.

So point to remember is static initialization block will be run only once the moment class file is loaded into JVM.Non-static initialization blocks will be run once for each object of the class created using new.There could be more than one static/non-static initialization blocks in the code.They are executed in the order in which they appear.

Hope this helps.

----Vishram
Hello everybody,

I am using the following code for demonstration of switch.(Reference K&B's book for SCJP 5.0 page 324)

public class SwitchDemo{
public static void main(String[] args){

final int a=10;
final int b;
b=2;

int x=0;
switch(x){
case a:
System.out.println("ABC");
break;
case b:
System.out.println("PQR");
break;
default:
System.out.println("XYZ");
break;
}

}
}

In this case,I am taking two final variables a & b.I assign the value 10 to the variable a right at the time of declaration.While in the case of variable b, I first declare it and then assign a value in the next line.

This program does not compile and the error I get is 'constant expression required'.

If I make slight change in the program and assign value 2 to the final variable b right at the time of declaration i.e. if I say final int b=2; and remove the line b=2, the program compiles and runs without any error or runtime exception.

In K&B's book it is written that case constants must be compile time constants.

The point I did not understand is what makes b a compile time constant if I assign value 2 to b right at the time of declaration? And what difference does it make if I first declare b to be an int type final variable and then assign value 2 to it? Afterall final variable is a final variable,whether we assign value right at the time of declaration or later on. Once a value is assigned to it,it cannot be changed.

So why does compiler complain in first case?

I would greatly appreciate help on this.

Thanks in advance.

----Girish
16 years ago
Thanks a lot Jesper.I made the change and got it working.

--Girish
16 years ago
Hello everybody,

I am using JCreator as an IDE for my java programs with classpath pointing to jdk1.6.0_02 folder.That means I am using JDK 6.

IN K&B's book for exam SCJP 5, on page 236 it is mentioned that the two DIFFERENT instances of Integer wrapper class will always yield true to == comparison when the undelying values of the two class objects are same and between -128 and 127. I coded the dollowing java program by combining the code given on pages 235 and 236 of K&B's book.


public class WrapperDemo2{

public static void main(String[] args){
WrapperDemo2 w=new WrapperDemo2();
Integer i1=new Integer(1000);
Integer i2=new Integer(1000);

Integer i3=new Integer(10);
Integer i4=new Integer(10);

if(i1!=i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningully equal");

if(i3==i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");

}
}

On Page 235,it is mentioned that for first two if statements,we should get the following output---

different objects
meaningully equal

On Page 236,it is mentioned that next two if statements produce output--

same object
meaningully equal

However I am getting the following output when I run the program--

different objects
meaningully equal
meaningfully equal

That means third if condition is false and hence that loop is not executed.

Could somebody throw some light on this?I am using JDK 6 version.Is it possible that JDK 5 will give one type of behavior and JDK 6 opposite?

Thanks

----Girish
16 years ago
If you have reproduced your code exactly here on this forum,I can see two mistakes for sure.

1) When you compile and try to run a java program,the execution of the program starts from main method.You have not defined a main method in your program.Of course that will not cause a compile time error.Absence of main method will not cause execution of the program at run time.

2)
You entered--

private StagIO io = new StaugIO();

Apparently you are trying to create a new object of the class type StaugIO but on the left hand side you are defining object belonging to class type StagIO. You can do this only if StagIO is a superclass of StaugIO class.But from your code, it does not seem to be.

See if this helps.

----Girish
16 years ago