Static Initialization

Hi All,

Is there anyway to initialize variables in a static class without using a static initializer block and like below:

private static int i= init()

private int static init()

{

....

....

return integervalue;

Thanks in advance.

Regards,

Suresh

}

[320 byte] By [Suresh_in80a] at [2007-11-27 5:54:39]
# 1
No, because that's exactly what static initalizers are there for.A third way would just be to set those values inside some method...
CeciNEstPasUnProgrammeura at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi,

Thanks for the immediate reply.

I got a situation like below:

StatciClass.init();

StaticClass.doSomeProcess1();

StaticClass.doSomeProcess2();

For Every Call i need to call init() method, i want the init() method to be inside my doSomeProcess() method....

For the two calls doSomeProcess the init() method is common.

Regards,

Suresh

Suresh_in80a at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 3
Why not write in an object-oriented fashion? Create objects and have this init() functionality in the constructor. It sounds like you are running and hiding from object-oriented programming.
Hippolytea at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 4
<firing up crystal ball/>"But if I make it an object, I always get the "cannot access non-static member from static context" error."
CeciNEstPasUnProgrammeura at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 5

Maybe what you want is a static initializer block.

The format is:

static {

// initialise here

}

Code in such a block is simply appended to the accumulated class initialization code and executed the first time the class is used.

malcolmmca at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 6
> Maybe what you want is a static initializer block.Reply #2 implies that the init code needs to be called more than once,like every time one should have created an object. Hmmm...
Hippolytea at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 7
Thats exactly my requirement i need to call the init() method each and everytime whenever the class has used.But without using the Static{ initializer} block.Regards,Suresh
Suresh_in80a at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 8
Problem solved. See reply #3.
Hippolytea at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 9
> <firing up crystal ball/>> > "But if I make it an object, I always get the "cannot> access non-static member from static context" error."LOL.
petes1234a at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 10
> Problem solved. See reply #3.Hi,I have the class as final so its not possible.Could u propose me some other method.ThanksRegards,Suresh
Suresh_in80a at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 11
> I have the class as final so its not possible.How does the class being final stop the initialisation being done per instance in a constructor?
pbrockway2a at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 12
> > Problem solved. See reply #3.> > Hi,> > I have the class as final so its not possible.> > Could u propose me some other method.reply 5, you idiot.
prob.not.sola at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 13

> > > Problem solved. See reply #3.

> >

> > Hi,

> >

> > I have the class as final so its not possible.

> >

> > Could u propose me some other method.

>

> reply 5, you idiot.

Is there anyway to initialize variables in a static class without using a static initializer block and like below

Do you think you are genious stupid what is the meaning of the text in bold in post?

First read throughly the posts and reply, otherwise shut your **** thing and do your work instead of thrwoing stupid words

Suresh_in80a at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 14

> First read throughly the posts and reply

Sounds like reasonable advice. So, what was the meaning of that first word in reply one again?

It has been suggested that truely static data be initialised in a static block. And that data that is different for each - well, instance - of the Class's use be initialsed in a constructor. And I can't, even after reading your posts ever so thoroughly, see any reason stated for departing from this standard practice.

pbrockway2a at 2007-7-12 15:49:21 > top of Java-index,Java Essentials,Java Programming...
# 15

> > > > Problem solved. See reply #3.

> > >

> > > Hi,

> > >

> > > I have the class as final so its not possible.

> > >

> > > Could u propose me some other method.

> >

> > reply 5, you idiot.

>

>

> Is there anyway to initialize variables in a static

> class without using a static initializer block

> and like below

>

> Do you think you are genious stupid what is the

> meaning of the text in bold in post?

>

> First read throughly the posts and reply, otherwise

> shut your **** thing and do your work instead of

> thrwoing stupid words

go **** yourself; you stupid piece of ****.

prob.not.sola at 2007-7-21 21:37:20 > top of Java-index,Java Essentials,Java Programming...
# 16
it's quite hilarious; to see a '.init' method here.come on *******; you are clasically emulating the lifecycle of a ******* object. you are an idiot.
prob.not.sola at 2007-7-21 21:37:20 > top of Java-index,Java Essentials,Java Programming...
# 17
mmm, stars.
prob.not.sola at 2007-7-21 21:37:20 > top of Java-index,Java Essentials,Java Programming...
# 18
Once again you have proved you r good for nothing so ************************** try to get rid of the forum soon, so others wont get irritated. And if you dont know anything just ************ and do your work. Dont think others are since u r like that..........
Suresh_in80a at 2007-7-21 21:37:20 > top of Java-index,Java Essentials,Java Programming...
# 19
I just have ******* to say about ****. When I ****** your ****, I didn't expect to ******. So ******* and the horse you ******* in with too!
-Kayaman-a at 2007-7-21 21:37:20 > top of Java-index,Java Essentials,Java Programming...
# 20

> I just have ******* to say about ****. When I ******

> your ****, I didn't expect to ******. So ******* and

> the horse you ******* in with too!

Thats exactly what i thought to tell you!!!!! :) you can try mine**********and the elephant with your********:). If yours to ***********then we will go for some artificial**********:). So then your*******will get good experience.

Suresh_in80a at 2007-7-21 21:37:20 > top of Java-index,Java Essentials,Java Programming...
# 21
What kind of fuckery is this?
Hippolytea at 2007-7-21 21:37:20 > top of Java-index,Java Essentials,Java Programming...
# 22

Will you*************with a rusty*************until it really hurts**************flagellate***************bleed*******infected************you stupid************even harder, like so it stings more than sitting********arsenic*************if you don't I'm going to get my dads************seen bladerunner 50 times or something***********all the words**********bigger than yours, tons************any more. Ok?

georgemca at 2007-7-21 21:37:20 > top of Java-index,Java Essentials,Java Programming...
# 23
lol
prob.not.sola at 2007-7-21 21:37:20 > top of Java-index,Java Essentials,Java Programming...