wood burning stoves 2.0*
The moose likes Java in General and the fly likes How the Interface works ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How the Interface works ?" Watch "How the Interface works ?" New topic

How the Interface works ?

Abdul Kader
Ranch Hand

Joined: Apr 11, 2007
Posts: 115
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
Ranch Hand

Joined: May 31, 2007
Posts: 3219
Try it yourslef by typing into an IDE. Also try it with break points in debug mode.

Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Abdul Kader
Ranch Hand

Joined: Apr 11, 2007
Posts: 115
i cant apply breakpoint to the interface code.....

i can't how the flow is executed... any help
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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 ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
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

Joined: Apr 11, 2007
Posts: 115
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

Joined: Mar 07, 2007
Posts: 1280

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.

Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
The assignments are only made once.
Unassigned int values are 0.

I modified the previous response with more detail.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: How the Interface works ?
Similar Threads
Cyclic class
a question about super command
how to be a human compiler
a problem with the resolt of the code
a bug with a constructor