• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How the Interface works ?

 
Abdul Kader
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering with the out which i got by the below program. Please let me know how the values are initialized in the following interface




Why the compiler is not saying this a Cyclic initialization
 
arulk pillai
Author
Ranch Hand
Posts: 3388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it yourslef by typing into an IDE. Also try it with break points in debug mode.
 
Abdul Kader
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i cant apply breakpoint to the interface code.....


i can't how the flow is executed... any help
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll notice that the output is



regardless of the order in which the output lines are placed.

Once it hits the cycle it uses the pre-defined value, which for an int is 0. (Some languages don't guarantee the 0 value, but Java does.)

A = 1 + B ... (let's not assign A, and go figure out B)
B = 1 + C ... (let's not assign B, and go figure out C)
C = 1 + whoa... (we're waiting to assign A, we'll use A=0 as a fallback)

assign C = 1
assign B = 1 + C = 2
assign A = 1 + B = 3

(edited for more detail)
[ May 15, 2008: Message edited by: Bill Shirley ]
 
Krishan Chauhan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill

If the logic that you are suggesting is true then it would mean reassigning a value to a final variable(constant) since all the variable declared in interface are implicitly final.
 
Abdul Kader
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You right Krishan, However i believe at very first time when it is accessing the Instance value it is not initialized (which Java takes as 0), then only the values are set.....

Correct me if i was wrong
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krisha, why do you think a value is re-assigned to the final variables? Actually I didn't know how this works. But with the description of Bill I'd say the compiler can't even assign any value to this variables until it detects the loop. So there's really only one assignment of a value.

Or am I wrong? Unfortunately I don't know how exactly the compiler does this work internally.

Marco
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assignments are only made once.
Unassigned int values are 0.

I modified the previous response with more detail.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic