LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   JAVA: Can an initialization block take parameters from the constructor? (https://www.linuxquestions.org/questions/programming-9/java-can-an-initialization-block-take-parameters-from-the-constructor-434880/)

byteframe 04-13-2006 02:08 PM

JAVA: Can an initialization block take parameters from the constructor?
 
Can one of those...

{
something = somethingElse;
}

'initlization blocks' take parameters, or know the parametrs sent in to the constructor?, or are they only useful for doing stuff like

{
alwaysStartsAtZero = 0;
}

Thanks.

FLLinux 04-13-2006 02:21 PM

I don't understand you question. Do you want to pass parameters to a class constructor?

byteframe 04-13-2006 02:45 PM

I'm passing arguments to a class constructor, and all of these constructors so far have been just assigning the data members to the parameters...I just read about the intilization blocks, and I thought it would be nice to use those instead (because thats what they're for), but I dont know if the code in the block has 'access' or can 'see' the parameter data passed in to the constructor.

graemef 04-13-2006 02:46 PM

Try it out...

but yes I believe so.

jlliagre 04-13-2006 03:56 PM

I believe they can't take parameters for at least two reasons:
- there is no way to pass parameters to these blocks
- they are executed before the formal constructors which receive the parameters

graemef 04-13-2006 05:19 PM

Sorry I was thinking of the initialiser list in C++ where you can do that, the initialiser block in Java is to provide default values and thus ensure that the variables are always initialised.

byteframe 04-13-2006 06:37 PM

Makes sense. I just remembered using the Initilizer lists in c++ and using them for initlization of the data fields, and having my constructors do nothing, as it seemed really cool and badass to do it that way.

Thanks.

jlliagre 04-14-2006 12:57 AM

Actually, there is no need to use these blocks to simply have variables initialized, as this can already be done outside any block but the class one.
The interest of these anonymous blocks is that it allows to have pieces of structured programming to be executed before the constructor is called.
Another close feature is the anonymous static block, which allows code to be executed once when a class is loaded.

byteframe 04-14-2006 08:51 AM

Thanks. As it turns out Java assigns default values to variables, and in my case I didnt even need the initilization block, as both my referance type and int variable had default variables I was fine with. (null, 0)

elyk1212 04-14-2006 10:51 PM

Quote:

Makes sense. I just remembered using the Initilizer lists in c++ and using them for initlization of the data fields, and having my constructors do nothing, as it seemed really cool and badass to do it that way.
Yes, but just because you can does not mean you should ;). Look into your requirements, then into design. There is likely a way to produce your desired affect with no need for unconventional constructs. Think of who may have to modify your code later, readability is important.

By the way, just as in most lexical languages (all I am familiar with), unnamed blocks can only access activation records and stack frames of blocks surrounding them (and in the case of C, the global space is included in this).
Therefore, they can only have access to variables local to themselves and 'outside', lexically. This means no parameters to unnamed blocks :(

Besides, if you want to pass parameters to a block, isn't that just a method/function/constructor anyhow, just with a (possible) prior invocation? Look into design.


All times are GMT -5. The time now is 06:30 AM.