JAVA: Can an initialization block take parameters from the constructor?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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
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.
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.