1

So I am creating a function which takes an Object parameter and I need to access the properties of the parameter like so:

private double property;
public void func(Object obj) {
    property = obj.prop;
}

But every time I try to do this it gives me the error: "'prop' cannot be resolved or is not a field." Is there a "right" way to do this? I am using Eclipse Mars.2 to edit this code if that helps.

Eric
  • 9,870
  • 14
  • 66
  • 102
Canvas16
  • 11
  • 6

3 Answers3

1

So I am creating a function which takes an Object parameter and I need to access the properties of the parameter like so

The parameter shouldn't be declared of type Object. It should be declared with the name of whatever class this code is in.

If you have a good reason for declaring it of type Object (for instance, you're overriding equals), you'd do a type check and then a cast:

if (/*...appropriate type check here...*/) {
    property = ((TheClass)obj).property;
}

But again, normally you shouldn't have to do that.


Re "appropriate type check here": The appropriate way to check for the type can vary. For instance, in many cases obj instanceof TheClass is good enough. But for overriding equals, you typically need something more complex, like this.getClass().equals(obj.getClass()). You use the right tool for the job in the code you're writing.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • *(Oops, if you see `getType` in the above, hit refresh. Obviously meant `getClass`. Some weird mish-mash of Java and C# in my head.)* – T.J. Crowder Jan 18 '18 at 17:59
  • If I declared the argument with a specific class, wouldn't the function then only accept arguments that are instances of that class? I have several objects declared in their own classes and the function should work for all of them. – Canvas16 Jan 18 '18 at 18:04
  • 2
    @Canvas16: *"If I declared the argument with a specific class, wouldn't the function then only accept arguments that are instances of that class?"* Yes. Which is important, since you want to access information which is specific to that class. *"I have several objects declared in their own classes and the function should work for all of them."* Then look into [coding to an interface](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface). I recommend you step back from your current task and work through some basic tutorials. – T.J. Crowder Jan 18 '18 at 18:06
0

Original Answer based on initial question: Your syntax is not javascript. Here is what the JS would look like:

var myObject = {
    myProperty: 'value',
    anotherProperty: 'some other value'
};

function func(obj) {
   var thisProperty = obj.myProperty;
}

Well... the tags were changed to Java... so the answer also changes...

Your property variable is a double value which does not have a "prop" property.

Let's say you have an Object like so:

public class MyObject {

    private double prop;

    public double getProp() { return this.prop; }

}

Instantiate an instance of MyObject

MyObject myObj = new MyObject();
myObj.setProp(13.2);

The method could look something like this (inside a class for this example):

public class MyService {

    private double property;

    public void func(Object obj) {
       if (obj!=null) {
            this.property = obj.getProp();
       } 
    }

}

Pass the instance to the method:

MyService.func(myObj);
daddygames
  • 1,880
  • 1
  • 11
  • 18
0

I think I have figured it out. I change the function to take a String parameter and change an Object variable to an instance of a different class depending on the value of the String parameter.

package main;

import creatures.Dune_Wolf;
import creatures.Rock_Jackal;
import creatures.Sandray;
import creatures.Talon_Snake;
import creatures.Wasteland_Wolf;

public class Combat {
    private Object enemy;
    /**
     * Instigates a battle scenario between 'player' and 'enemy'.
     * @param enemy
     */
    public void battle(String Enemy) {
        if (Enemy.equalsIgnoreCase("dune wolf")) {
            enemy = new Dune_Wolf();
        } else if (Enemy.equalsIgnoreCase("rock jackal")) {
            enemy = new Rock_Jackal();
        } else if (Enemy.equalsIgnoreCase("sandray")) {
            enemy = new Sandray();
        } else if (Enemy.equalsIgnoreCase("talon snake")) {
            enemy = new Talon_Snake();
        } else if (Enemy.equalsIgnoreCase("wasteland wolf")) {
            enemy = new Wasteland_Wolf();
        }
    }
}
Canvas16
  • 11
  • 6