Home » questions » Java Question: How do I get access an object's variables?

Java Question: How do I get access an object's variables?

2006-08-09 09:25:35, Category: Programming & Design
Say I have a class, and I want to pass an object. Time t. How would I go about accessing variables h and r inside the object? public int elapsedSince(Time x, Time y) Both X and Y are objects. I need to get Hours and Minutes out of both of them.

Answers

  1. Ed

    On 2006-08-09 10:15:35


    Your Time class should have methods like "getMinutes()" and "getSeconds" in which you would return the values of the internal fields for that instance. Then your other method that accepts Time objects named 'x' and 'y' could do something like: diff = x.getMinutes() - y.getMinutes() to compute the difference in minute values. I wouldn't recommend using public fields to access the data -- keep the fields private and use accessor methods.
  2. rickunlimited1952

    On 2006-08-09 09:35:48


    find java folder in ur C: drive click properies
  3. Kryzchek

    On 2006-08-09 09:46:59


    Your question is a little unclear. You need to create Accessors for your object's properties if they are being used outside of the class. After that, you just use simple dot-notation to access an object's properties. IE: Time x = new Time(); x.Hours x.Minutes