LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2006, 02:08 PM   #1
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Rep: Reputation: 15
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.
 
Old 04-13-2006, 02:21 PM   #2
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
I don't understand you question. Do you want to pass parameters to a class constructor?
 
Old 04-13-2006, 02:45 PM   #3
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
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.
 
Old 04-13-2006, 02:46 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Try it out...

but yes I believe so.
 
Old 04-13-2006, 03:56 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
Old 04-13-2006, 05:19 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 04-13-2006, 06:37 PM   #7
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
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.
 
Old 04-14-2006, 12:57 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 04-14-2006, 08:51 AM   #9
byteframe
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 62

Original Poster
Rep: Reputation: 15
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)
 
Old 04-14-2006, 10:51 PM   #10
elyk1212
Member
 
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
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.

Last edited by elyk1212; 04-14-2006 at 10:54 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No virtual constructor in C++ Why??? krishnacins Programming 10 04-11-2006 09:19 PM
No parameters displayed using ps for java processes beegster Red Hat 0 11-23-2003 05:04 PM
c++ constructor question jhorvath Programming 6 11-06-2003 10:52 PM
passing java parameters to a unix script nephilim Programming 25 10-22-2003 10:51 AM
Anal retentive initialization for Java... JStew Programming 11 05-28-2003 03:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration