aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface Q" Watch "Interface Q" New topic
Author

Interface Q

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Consider the following code :



O/p:
It will print j = 3, jj=4 and then 3.
I was expecting j=3 as the o/p.
How is it that jj=4 and 3 are also printed as part of the o/p?
kenzo testini
Greenhorn

Joined: Mar 10, 2005
Posts: 2
Kedar,
it prints out jj=4 because when the interface constant j is referenced by K.j the interface J's <init thread> resolves all its variables, which are calls to the static method "Test.out"(which happens to have a System.out.println), so any other variable you define with Test.out in an interface you refer to is going to be println'd , too.
Notice that if you call System.out.println(K.i) it will do the equivalent to the interface constants of I.

3 is printed out because that is what your main prints out:
System.out.println(K.j) = System.out.println(Test.out("j",3)
First Test.out calls System.out.println which yields j=3
then it *returns* the int 3, which is printed out by the
System.out.println in your *main* method
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
According to JLS,
jls, initialization

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:


T is a class and an instance of T is created.
T is a class and a static method declared by T is invoked.
A static field declared by T is assigned.
A static field declared by T is used and the reference to the field is not a compile-time constant (�15.28). References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.


also,

A reference to a class field causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface


Looking at the statements from JLS above gives u ur answer.
U can see the class or interface gets initialized when the static field declared by a class is used. Since in interface the variables are implicilty static final, the interface gets initialized.
The second quote from jls implies that the interface being referred is only initialized.

Hope u got the answer
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi all,

i've modified the code a little bit, all the explanation given is valid but then why is the o/p now



code
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi all,

i've modified the code a little bit, all the explanation given is valid but then why is the o/p now :
j=3
jj=4
z=9
3
1

instead of:
j=3
jj=4
z=9
3
1
ii=2



codecode
interface I
{
int i = 1, ii = Test.out("ii", 2);
}
interface J extends I
{
int j = Test.out("j", 3), jj = Test.out("jj", 4);
int z = Test.out("z", 9);
int y = 8;
}
interface K extends J
{
int k = Test.out("k", 5);
}
class Test
{
public static void main(String[] args)
{
System.out.println(K.j);
System.out.println(K.i);
}
public static int out(String s, int i)
{
System.out.println(s + "=" + i);
return i;
}
}

thanx
amit
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
good Question Amit
Well, about the answer to ur doubt, please carefully have a look at the points in the jls i have noted down:

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

T is a class and an instance of T is created.
T is a class and a static method declared by T is invoked.
A static field declared by T is assigned.
A static field declared by T is used and the reference to the field is not a compile-time constant(�15.28). References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.


When u execute statement K.i, since i a compile-time constant, the interface doesnt get initialized(look at he fourth point, highlighted one). So, the print statement what u expected doesnt get to execute.

Hope u got my point.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
godit Animesh!!!
thanx for showing me the light
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206

//output

//Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device

Can you tell me why initally the String variavle 'device' is null despite of the fact that the constructor of the super class is called and we String variable 'device' declared there also. So it should be initialized as soon as the constructor call starts because the super class has to initialize first and than the construcor executes.
Correct me if i'm wrong..

thanx
amit
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

Can you tell me why initally the String variavle 'device' is null despite of the fact that the constructor of the super class is called and we String variable 'device' declared there also.

U know that the method showDevice() in the constructor of Phone class being called is not of the Phone but Mobile class. Thats why device in the Mobile class is uninitialized then and so u get null as the device's value.
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
i got the followingout put

Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device

which is perfect .. no nulls
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

Oh, u get the null in the output for the above piece of code.
I am sorry , i didnt look at the question properly.
Thanks
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
i got the output like this

Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device

i have no idea where the constructor of phone class has been called..
since there is no Phone.showDevice
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
sorry Animesh and Aruna actually i wanted to put the code put by Animesh only but it was me who modified the code in such a manner that we dont get a null,.......copy past error u see...
well i got the answer later that day after detialed analysis of JLS......

animesh,
r u refering JLS or you are reading it as we compelte some buk>>???

thanx
amit
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Parameswaran,
Its the constructor of the base class i.e. Mobile class which calls the constructor of Phone class there by calling showDevice() method.
Hope u got it.

From Amit,

r u refering JLS or you are reading it as we compelte some buk

No i only refer JLS whenever u all send me some doubts. Its difficult to study the whole of JLS, but some topics in JLS are worth reading, so u can go thru it. Sometimes even i dont know the answer to some solution posted in the forum, i try to find it out in the best way i can, go thru jls or try out myself, and thats how i too gain knowledge from these.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
cheers!!!
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
animesh,

do u have any idea gettting a SCJP cert will benifit how much ??
i mean are there companies which give preference to SCJP's....
SCJP is more abut concepts and less about project xprience

thanx
amit
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

do u have any idea gettting a SCJP cert will benifit how much ??

Lots of benefit on doing SCJP. U will be preferred over people who havent done SCJP, certified by itself means ur concepts are clear on that subject. Project Experience will come to u when u start working. And if before starting work u r a certified programmer, u r the most preferred one.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
yeah i also think the same ....
just girding up my lions
In fact there is some company in the US which takes only SCJP's.....
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
Animesh
thanx i got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface Q