1

I am actively learning so bear with me but, for example, let's say I have a constructor (with no parameters) of a player that holds points and name. When I go to a DIFFERENT class, and I make an object out of this constructor how would I, later in the game, change the parameters like score from 0 to, I don't know, a 100. How would I do that. I tried searching in google but no one gives an answer.

Omar Hossain
  • 37
  • 1
  • 3
  • 4
    You need to understand the use of methods, in this commonly known as "getters" and "setters", have a look at [Classes and Objects](https://docs.oracle.com/javase/tutorial/java/javaOO/index.html), in particular [Defining Methods](https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html) and [Passing Information to a Method or a Constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) – MadProgrammer Nov 24 '15 at 03:46
  • This will helpful for you http://stackoverflow.com/questions/9991503/object-parameters-in-a-constructor – Kasun Gamage Nov 24 '15 at 04:04
  • Please checkout my answer, hope it helps you! – SyntaX Nov 24 '15 at 04:10

2 Answers2

1

User.java

public class User {

    private String name;
    private int points;

    public User() {
        this.name = name;
        this.points = points;
    }

    //Getting of value points
    public int getPoints() {
        return points;
    }

    //Setting of value points
    public void setPoints(int points) {
        this.points = points;
    }

}

OtherClass.java

public class OtherClass() {
    User user = new User(); //Calling of constructor
    user.setPoints(100); //function setPoints being called; setting value to 100
    System.out.println(user.getPoints());
}
nubteens
  • 5,462
  • 4
  • 20
  • 31
0

First of all you have to understand the concept of Encapsulation in java. Here the variables of a class are declared as private and public setter and getter methods to modify and view the variables values are provided. Pleases follow these links for further reference:

Player class:

public class Player {

    private String name;
    private int score;

    public Player() {
    }

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

    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Player [name=" + name + ", score=" + score + "]";
    }
}

Test class:

public class TestPlayer {
    public static void main(String[] args) {
        Player player = new Player();
        player.setName("Omar Hossain");
        // set score 100
        player.setScore(100);

        System.out.println(player);

        // increment score by 10
        player.setScore(player.getScore() + 10);
        System.out.println("Updated score of player: " + player.getScore());
    }
}
Community
  • 1
  • 1
SyntaX
  • 2,090
  • 17
  • 30