Is it possible? (not a monster?)

G

giob

Could someone examine this code I made while studying and explain what
it does and why do you think is possible to generate this kind of code?
Shouldn't it give a compilation error ?


class Frankenstein{
static Frankenstein x = Frankenstein.getOneConn();

static Frankenstein.getOneConn() {
return x;
}
}


thanks
 
T

Timbo

giob said:
Could someone examine this code I made while studying and explain what
it does and why do you think is possible to generate this kind of code?
Shouldn't it give a compilation error ?


class Frankenstein{
static Frankenstein x = Frankenstein.getOneConn();

static Frankenstein.getOneConn() {
return x;
}
}
Sounds a bit like a homework assignment to me. For starters, it
does give a compiler error. Assuming the 4th line should be:

static Frankenstein getOneConn() {

BUT... why don't you tell us why you think the compiler should
generate an error?
 
R

Roedy Green

class Frankenstein{
static Frankenstein x = Frankenstein.getOneConn();

static Frankenstein.getOneConn() {
return x;
}
}

There are two things peculiar about the cod: the circularity and the
use of the class name in the method definition.

Java does nothing at all to protect you from illogical initialisation
or to do your initialisations in natural order like a spreadsheet.

Your code would just set x to null. See
http://mindprod.com/jgloss/initialisation.html for why.
 
T

trippy

giob said:
Could someone examine this code I made while studying and explain what
it does and why do you think is possible to generate this kind of code?
Shouldn't it give a compilation error ?


class Frankenstein{
static Frankenstein x = Frankenstein.getOneConn();

static Frankenstein.getOneConn() {

you need a return type for your method

return x;
}
}


thanks

--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Doughnut Song" -- Tori Amos

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"
 
G

giob

sorry , it was
static Frankenstein getOneConn() { // with a space instead of .
(dot)

the problem is that the static method returns a class variable

but the class variable is initialized by the use of this static method


class Frankenstein{
static Frankenstein x = Frankenstein.getOneConn();
static Frankenstein getOneConn() {
return x;
}
}
 
C

Chris Uppal

giob said:
Could someone examine this code I made while studying and explain what
it does and why do you think is possible to generate this kind of code?
Shouldn't it give a compilation error ?

There's no reason why it should give a compiler error. Forward references
(references to fields and methods which are defined later in the class) are
allowed in Java, and other than that there's nothing for the compiler to worry
about. It may be weird code, but the compiler doesn't care about that, any
weirdness is /your/ problem ;-)

So the code does just what it says it does. Nothing "mysterious" happens, so
as the class is initialised, 'x' is assigned to the (then) current value of
'x'. Which is null. What else would you expect to happen ?

-- chris
 
G

giob

ok thank you.
It just sounds me weird to intitialize a variable with the value of
itself not already initialized :)
I'm happy to hear this community is so well haunted and gentle
Thank you everybody

Ciaoooo
viva l'italia col suo il vino rosso, la pizza, gli spaghetti, la
musica, il sole ...etc etc ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top