• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Defining user defined var.

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lionel: Very clever approach! I like it...
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

reply
    Bookmark Topic Watch Topic
  • New Topic