3

I am a beginner of Java. I have a question.

Foo.java

public class Foo {
    private String name;
    private int num;

    Foo(String name, int num) {
        this.name = name;
        this.num = num;
    }

    public String getName() {
        return this.name;
    }

    public int getNum() {
        return this.num;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNum(int num) {
        this.num = num;
    }

}

Main.java

public class Main {
    public static void main(String[] args) {
        String name = "Tom";
        int num = 60;
        Foo bar = new Foo(name, num);
    }
}

Instead of this typical style of class on Java, I'd like to set and get instance properties with getter() and setter() method like...

public void setter(String p) {
    this.p = p;
}

public String getter(String q) {
    return this.q
} 

I know these don't work, but I want to write Java method like following these codes in Python.

setattr(self, key, val)
getattr(self, key)

Would you please give me some pieces of advices?

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
yamachan
  • 1,029
  • 2
  • 12
  • 28
  • 2
    Do you want to input any value to the `setter` method? – wake-0 Aug 17 '16 at 12:17
  • could you explain the reason behind this, or why you would like to not use the java standard? – SomeJavaGuy Aug 17 '16 at 12:22
  • 2
    You could use Reflection API to write universal getter and setter methods that find fields by name. But its a bad idea in your case and you will have problems with types. So I would suggest to stick with java code standard if you want to code java. – ArcticLord Aug 17 '16 at 12:23
  • @Kevin Thank you for your comment. I want to use `setter` in the constructor because I don't want to write every properties even when I make an instance like `Foo bar = new Foo(name, num);`..haha I could implement codes in `Python`, `PHP`, `Go`, so I usually use `setter()` in constructor. – yamachan Aug 17 '16 at 12:52
  • @ArcticLord Thank you for your comment. I checked about `reflection`. in some web pages, they don't recommend to use `reflection`. Maybe I must follow Java standard way. – yamachan Aug 17 '16 at 12:54

2 Answers2

4

Java does not support properties.

The only way to do this is through accessor and mutator (get/set) methods like you did at the top.

See: does java have something similar to C# properties?

Community
  • 1
  • 1
byxor
  • 5,930
  • 4
  • 27
  • 44
  • The answer is a bit rough and will require to go check associated question, meanwhile it is mainly correct : java do not provides access through static methods to instances variables. – Riduidel Aug 17 '16 at 12:33
  • Thank you Brandon. That is why I couldn't find information in Japanese when I searched about that. And thank you for your link. – yamachan Aug 17 '16 at 12:44
  • Thank you for your comment, Riduidel. I understand. – yamachan Aug 17 '16 at 12:45
1

What you want to achieve can be done using reflection as next:

// get the field "key" from the class "Foo"
Field field = Foo.class.getField("key");
// make it accessible as it is a private field
field.setAccessible(true);
// set the value of this field to val on the instance self
field.set(self, val);

More details about reflection here

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122