globals and custom functions in applets

Started by
0 comments, last by ApochPiQ 18 years, 4 months ago
My program uses custom functions that I define and declare. However Java requires that all methods be contained inside of a class. I went ahead and made a class called CustomFunctions, which has all of the custom methods my program uses. I haven't compiled it yet (too many other errors), but I am assuming that in order for another function to gain access to my custom functions, I simply need to pass the function an instance of my CustomFunctions class and use the dot-operator to reference a method of the CustomFunctions object. Example: class CustomFunctions { int method1() { /* blah */ } int method2(byte b) { /* blah */ } // ... } // ... inside JApplet OR any other class for that matter CustomFunctions cf = new CustomFunctions(); int x = cf.method1(); But, surely there must be a better/easier way of doing this.... Can I just write my own custom functions inside the applet class instead of making my own class, or does JApplet only allow the life cycle method(init, start, stop, destroy,etc.) to be defined? Is there an easier way of going about all this?? Also, I defined several constants in my JApplet class that, in C++, would be globals for all the functions to use. Example: // inside CustomFunctions class' method2() int method2(byte b) { if(b == CONSTANT_1) {} else if(b == CONSTANT_2) {} // CONSTANT_1 and _2 defined in JApplet // ... etc. } How do I make the other Java classes (other than JApplet) aware of these constants so I can check for their values in conditional statements?
Well I believe in God, and the only thing that scares me is Keyser Soze.
Advertisement
I'm not very familiar with Java, and not at all familiar with JApplet, but I think what you're after is "static final" member variables. I'm not sure how that works out for member functions, but IIRC you should be able to do something similar there as well. In any case checking your Java documentation for "static" and "final" should at least point you in the right direction.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement