File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Defining user defined var.

 
Emnaki Chih
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Emnaki Chih
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Emnaki Chih
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Lionel Badiou
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Emnaki,

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



Hope that helps,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lionel: Very clever approach! I like it...
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use an interface to define constants like this! Use a class, and then you can use the following:

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic