aspose file tools*
The moose likes Beginning Java and the fly likes Defining user defined var. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Defining user defined var." Watch "Defining user defined var." New topic
Author

Defining user defined var.

Emnaki Chih
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
in C there is a function #define i think which acts like precompile substitution
is there something like that in java?
I would like to initialize a lot of arrays in an interface. Most of the elements in the array are related and will most likely appear if the other one(s) appears as well. Is the a faster more maintainable way of doing this in java appart from enter in the intialization of each array in full?
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
Originally posted by Emnaki Chih:
in C there is a function #define i think which acts like precompile substitution
is there something like that in java?


no, there isn't. (and, in fact: no, there isn't - C's #define is not a function, properly. functions can't be called before compilation.)

I would like to initialize a lot of arrays in an interface. Most of the elements in the array are related and will most likely appear if the other one(s) appears as well. Is the a faster more maintainable way of doing this in java appart from enter in the intialization of each array in full?


i wish i knew of a good answer to this, but i'm just not sufficiently familiar with interfaces. i don't think you can run code in an interface, so this might actually be a sign that your design is not quite optimal; are you sure that this initialization couldn't somehow be moved into a class?
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Hi Emnaki,

As for myself, what you are trying to do is to vague to offer an hint. Could you post a C sample code of what your are trying to do in Java ?

Best regards,


Lionel Badiou
CodeFutures Software
Emnaki Chih
Ranch Hand

Joined: Sep 01, 2004
Posts: 30


Won't it be great if I could add content to each list that needs it, and be able to add to content and automatically add to all the lists with contents and not have to add to add to each initialization one by one? These are all declared in the interface by the way.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Can you restate your question? The last message is not very clear to me. It would be helpful if you wrote the code first in C using the #define to illustrate what you would like to do in Java. C's #define sytnax does a lot of different things, and depending on how you are using it, there are different approaches to use in Java to get similar results.

HTH

Layne


Java API Documentation
The Java Tutorial
Emnaki Chih
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
Lets say java has the define function then I could do.

instead of

is there a way to do this in java? it will help if I have to put CONTENTS into a lot of array initializations.
[ February 09, 2005: Message edited by: Emnaki Chih ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Could you use an ArrayList instead of an Array?

In the code below, I've defined an interface with a pre-valued List (since you indicated above that you might want to use an interface in this manner).

Then I defined a class implementing that interface, and used the List to initialize an ArrayList. After that, I can add to the ArrayList as desired.


This code uses 1.5. But I think the only new feature here is the generics, so if you remove the type parameters <String> it should work in 1.4.
[ February 10, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Hi Emnaki,

Here's a sample code (1.4) that you could do.



Hope that helps,
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Lionel: Very clever approach! I like it...
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

Don't use an interface to define constants like this! Use a class, and then you can use the following:



Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
jQuery in Action, 2nd edition
 
subject: Defining user defined var.