• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Passing Parameters

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am reading in data from a file, three pieces of data actually, all in an integer format. I want to then pass these three pieces of data into another seperate java file.
Ideally I think I would like to do it using classes rather than putting the three pieces of data into a concatenated string and passing it through that way.
Any ideas??
Thanks!
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cross-posted
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post messages to more than one forum. Thanks. I've closed the other copy of this post.
 
Shane Lilly
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies, wasn't sure whether it was deemed an intermediate or beginner question!
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shane Lilly:
Hi,
I am reading in data from a file, three pieces of data actually, all in an integer format. I want to then pass these three pieces of data into another seperate java file.
Ideally I think I would like to do it using classes rather than putting the three pieces of data into a concatenated string and passing it through that way.
Any ideas??
Thanks!


You can use a 3rd approach. Pass each as an integer as arguments to the contructor. Which is better depends on what you want to accomplish, what the 2nd java file is doing to these 3 ints, what these 3 ints represent, whether these 3 ints can be considered as attributes of some object etc.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm somewhat confused on what your question is exactly. If you are programming in Java, you obviously have to use classes, and each separate .java file has at least one class in it.
Are you saying that you want to wrap the three values in ANOTHER class that can be passed as a parameter to its destination? This might make sense. If I were working on this project, I might first ask myself if these numbers are related in some way. That is, do they have some meaning as a group?
I'm having difficulty thinking of any examples that might make my comments clearer. Perhaps you can add some specifics about the classes you have so far and explain what you are trying to accomplish with a little more detail. That will make it easier to come up with a clear example that applies to what you are doing.
Layne
 
Shane Lilly
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, i will make myself a little clearer...
Within my original 1st java file I am using a form of openGL and java binding called JOGL. This is a 3d programming interface. Within this 1st java file i aim to complete 3d programming and drawing aspects.
From the first java file i am calling another java file to read the contents of a midi file, i.e. the musical note values, timings, etc.
Upon getting these values within the 2nd java file i then want to pass these back to the original 1st java file where i can use these parameters as variables to be used in the 3d drawings.
There will be probably at most three kinds of integer values that i will want to pass to the 1st java file. These integer values will represent musical events from the midi file.
Im just having problems passing them through...
so hopefully this explains things a little better. Any help would be gratefully appreciated!
Thanks
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi shane,
you can do it in two different ways. the first one is to pass it in the constructor of the class in which you want to use it. but this is'nt the preferred way (since you have to pass 3 values).
the second method is to make these 3 values as properties (by using get-set). since yuor other class is in same project you can access it directly there.
here is a snapshot.
public String aaa;
public String getaaa()
{
return aaa;
}
public void setaaa(String X)
{
aaa = X;
}
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why shouldn't you pass three values to a constructor?
And if they are used as int-Values, it wouldn't be to good to handle them as String.
I wonder a little that programmers deal with midi-Files and OpenGL when they are beginners...
 
Lalit K Kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi stephen,
I did'nt said that you should'nt pass the values to the constructor. i said that it is not the preferred way. In our case there are only 3 variables so we can also pass it through constructor. but lets say we want to pass some 10 variables. then what will you do? pass all the 10 variables through constructor. In that way your code will look dirty.
In the code snippet I posted , I just took an example of passing a string value. If you want to pass any other datatype , you can do so by simply replacing the keyword "String" by your "required datatype"
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shane,
If you were to post relevant code examples of what you're working on, folks around here might be better able to help guide you in the right direction.
 
Shane Lilly
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are a few code fragments of what i am doing...
myOpengl.java

MidiReceiver.java

What I want to do instead of printing the values on the console, I want to take them as they are extracted from the midi file and then pass them into the first java file (myOpengl.java) so that i can then use them as variables so that i can plot 3d shapes with them....
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ February 25, 2004: Message edited by: Dirk Schreckmann ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that cleared things up a bit, I think. Are these three values the x, y, and z coordinates of a point? If so, you should create a Point class that wraps them up. This way you can pass and/or return the three values as one consistent unit.
If they are related in some other way, a class similar to this may make sense as well. Otherwise, you probably should use one of the other suggestions given above.
Layne
 
Shane Lilly
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so could anyone give me a real idiot proof guide of how to go abt my problem...
im thinking that maybe i am blowing it out of proportion and that its quite simple, but it doesn't seem that way to me at the minute!
i think i understand what a few of the posts are trying to do, but im sorry to say that i am stuck as to how to implement these strategies into my code...
thanks
 
Shane Lilly
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
really all i want is to be able to take the values that are extracted from the class 'decodeMessage' and then pass them into the myOpengl.java file so that with the myopengl.java file they can be implemented as global variables.
sorry for all the confusion, i've been told before that im not very good at explaining myself!
 
Lalit K Kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi shane,
I really could'nt found which parameters do you want to pass. but lets say you want to pass variable x (of int type). then in your "decodemessage" class set the properties.
public int X;
public int getX()
{
return X;
}
public void setX(int temp)
{
X = temp;
}
Similarly for all your other variables. you can write this code at the end of you current code.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit confused by this one, seems to me like there's some confusion between the definition of a java class and a java object, but here's my input anyway
It seems that the obvious answer is to have 3 variables declared in the MyOpenGL class like this
(in MyOpenGL.java)

which you then set using dot notation from the MidiReceiver class on an object of type MyOpenGL

However, to do this you will need access to a MyOpenGL object (which I've called my_ogl_object in the code above. Whether the right place for that is in MidiReceiver will depend on how the rest of your program works, but probably the best way to approach it is from another controlling class that has access to instances of both the MidiReceiver and MyOpenGL classes.
That way you can define 3 int variables in both classes and from the controlling class you can set the variables in MyOpenGL using the values in MidiReceiver.

Hope I haven't just made things more confusing
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of giving your MidiReceiver a PrintStream, give it a simple event listener. Every time the MidiReceiver runs across some kind of data that needs to be sent back to the other class, it simply calls the appropriate method on the event listener which passes it back to class that originally installed that event listener.


You could provide whatever information is appropriate for these midi events (obviously you'd rename them to something more appropriate, as well as their properties x, y, and z).
Then, when you create a new MidiReceiver:

Then, in MidiReceiver, whenever a midi 1 event occurs, the MidiReceiver instance simply calls midi1EventOccurred() on the MidiEventListener it was handed at construction time.
This even dovetails nicely with your current approach--you could simply implement the process methods to simply print out what has occurred to console if that's the functionality you want. You can create as many different kinds of midi events as you want, you can create a whole hierarchy of different midi event listeners instead of just the anonymous class implementing the interface method I showed above.
Have fun!
sev
 
Shane Lilly
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm finding it hard to follow that! i.e. where to implement that into my code!
What I have at the minute is I am able to pass through one value from the 'public String decodeMessage' in MidiReceiver.java by calling a method in myOpengl.java 'getIntValues(iMidiValue)'
the code for the getIntValues is as follows...
public static void getIntValues(int val1) {
System.out.println("myOpengl midivalue = " + val1);
}
This prints out the console fine... but when I try to make a public variable equal to the integer passed into the static method above, I get the error, 'Non-static variable cannot be referrenced from a static context'.
Is there any way to get around this??
I have already tried changing the method to 'public void getIntValues' but when I run the program and it tries to pass the integer from decodeMessage method in MidiReceiver.java to getIntValues method in myOpengl.java I get a java.lang.IncompatibleClassChangeError
I think this is because the whole MidiReceiver.java file is being called from a static call in myOpengl.java
'private static Receiver midi_receiver = new MidiReceiver()'
So ultimately is there any way to make a static variable public to the rest of the class??
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem you're having is that you're trying to set an instance variable of a particular object without knowing what instance you're dealing with. (That's what the error message you're getting means.) You have to understand what it means for something to be static, and then I think you'll see what's going on here.
Let's say you write a Dog class. For the sake of simplicity, let's say a dog only has a name in your application, so you'd make name an instance variable and provide a name setter and a name getter. Now let's say you want to make sure you keep count of the total number of dogs ever created in your application (for this app, you don't care how many are still "alive"--have objects roaming around the system--you only want to track how many have been new'd up). How would you do this?
Well, you might say, just include a numDogs variable and increment it whenever a new Dog instance gets constructed. Ok...

Do you see the problem with the above code?
The problem is, if you create two dogs, rover and bowser, they'll both be carrying around a numDogs variable that's equal to 1. This doesn't tell you how many total dogs have been created. Furthermore, when those two instances are gc'd, the numDogs variables die with them.
Instead of attaching that numDogs variable to a Dog instance--that is, a specific dog, like rover or bowser--what if you could attach it to the Dog class itself? That would mean it wasn't owned by any one dog, it would exist independently of any one dog. You can...just make it static:

Now, every time you write a statement like Dog rover = new Dog(), the numDogs variable that's stored up at the class level gets incremented. If you think of a class as a rubber stamp, and an instance as the ink left behind by that stamp, you'll quickly see the difference. You stamp out two dog imprints, rover and bowser, and each ink imprint has data associated with it that can change independently of the other. Rover's name is set to "rover" and bowser's to "bowser"--that data is unique to each instance.
Declaring something static, though, means you're not talking about the imprints left behind by the stamp, that's data associated with the stamp itself. In the code example above, numDogs is now attached to the stamp, so every time you plunk out a new dog instance, that counter on the stamp goes up by one.
Also note the way I've referred to the static data. Normally, when you access a data member, you'd say instance.data. But note that I've written Dog.numDogs. Dog is the *class*, not an instance like rover or bowser. Java confusingly will let you refer to static data using an instance reference (which I think I know why, but still don't agree with), but to keep things simple and clear to other developers I never do that. Besides, what if there are no instances available?
That's right...think about it. Even before the stamp has stamped out a single instance, you should be able to get access to that numDogs variable and see that it's zero, right? Well, you can...since you don't require an instance to get at it, you can just say: Dog.numDogs.
Static methods are similar--they are behaviors attached to the class, not to any specific instance of that class. Normally, you write a method and it has access to all of the instance variables. For example, the getNumDogs() method in the second example (note that I snuck a "static" in there too) seems like I should be able to add code that prints out the name of the dog, right? Wrong...think about it. If I said, S.o.println( name ), how would the static method know which instance I'm talking about, bowser or rover? It doesn't know--it's attached to the stamp, not a specific inking. So, static methods require that an instance be passed into them if you want them to operate on those instances. If you try to reference a non-static variable or method from a "static context"--inside a static method--you'll get the error message you got.
Make sense?
sev
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic