• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Interface Q

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
godit Animesh!!!
thanx for showing me the light
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cheers!!!
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Animesh
thanx i got it
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic