-1

Good Day Stackoverflow people!

I stumbled upon a simple problem and I can't find a solution to it by myself.

There are two methods in my Transform class with variables which I want to call in another class but I can't figure out how:

Transform Class Methods:

public Vector3f GetPos() {       
    return m_pos;
}

public void SetPos(Vector3f pos) {
    this.m_pos = pos;
}

The variables "m_pos" and "pos" are coordinates of a Vector3f which I need to use in a method called ProcessText which is in a different class:

Class2 Method:

public void ProcessText() {       
    String file_name = "C:/Users/Server/Desktop/textText.txt";

    try {
        ProcessCoords data = new ProcessCoords(file_name);
        data.writeToFile("makeGrass:0,1,2");

        System.out.println("Coordinates Saved!");
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

Now instead of having the ProcessText method write to the file "makeGrass:0,1,2" I want it to use "m_pos" and "pos" from the methods in my Transform Class, so I can say:

data.writeToFile("makeGrass:" + m_pos + pos);

However I have no idea how to do get the variables "m_pos" and "pos" from both methods of my Transform Class, if anyone could help me out I would be very happy.

beresfordt
  • 5,088
  • 10
  • 35
  • 43
DisasterCoder
  • 477
  • 1
  • 3
  • 17

3 Answers3

0

OK. First thing first: pos is not a member variable but it is a parameter in the method named setPos.

And in order to use m_pos, all you need to do is to have an instance of the Transform class in your 2nd class, let's say it is transform and then you need to call

transform.GetPos()

to use it.

Alp
  • 3,027
  • 1
  • 13
  • 28
  • 2
    *"pos is not a variable but it an argument"* If we're going to get picky about terminology, in Java they're called [*parameters*](http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.1), not arguments. :-) – T.J. Crowder Jul 19 '15 at 09:35
  • Thanks, I need SetPos too since GetPos always prints a bunch of empty coordinates while SetPos only prints coordinates when I push the enter key, if I use GetPos it will always give me (0,0,0).. is it even possible to use SetPos in this case? – DisasterCoder Jul 19 '15 at 09:40
  • @T.J.Crowder - aside from the fact that it IS a variable. A parameter is just one kind of variable. This is a very confused answer. – Dawood ibn Kareem Jul 19 '15 at 09:48
  • @T.J.Crowder you are right. Thanks for the correction. =] – Alp Jul 19 '15 at 10:21
  • @DisasterCoder By variable, I meant member variable. I thought it was quite obvious from the context but looks like I was wrong. – Alp Jul 19 '15 at 10:24
0

The method in Transform class are getter and setter method they both access same variable so you can not have different value for an instance variable at same time

ublic Vector3f GetPos()
{       
    return m_pos;
}

public void SetPos(Vector3f pos)
{
    this.m_pos = pos;
}

and to access this field all you need to create object of Transform class and call the setter and getter eg.

Transform obj = new Transform();
obj.SetPOS(pos);

and to access this value

obj.GetPos();
CodingRat
  • 1,934
  • 3
  • 23
  • 43
0

So I've found stuff out by myself and stumbled upon a different problem and made a new answer which described more accuratly what I wanted to achieve.

What I wanted to use is my GetPos method but I shouldn't do that from my Transform class - althought it would be feasible. I've come up with a different thing: Turns out that my Input class extends the Transform class and I am able to call GetPos() from there.

I've solved the issue now, so for more details on the matter go look here: Java using code from method doesn't work

Thanks for any help so far!

Community
  • 1
  • 1
DisasterCoder
  • 477
  • 1
  • 3
  • 17