Who can explain me? I solved this task, but I have some questions. The argument PERSON.AGE call the class PERSON and the variable AGE. Correct?
So, first I understand. But last one - no. Why we use the same argument PERSON.AGE and we receive another value? Already it's increased by 20.
Kirill
Level 22
how does this work?
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Денис Enterprise Java Developer
9 November 2022, 20:31
You should provide some code to review if you are expecting an answer.
0
Kirill
9 November 2022, 23:16
Person person = new Person();
System.out.println("Age: " + person.age);
person.adjustAge(person.age);
System.out.println("Adjusted age: " + person.age);
}
public static class Person {
public int age = 20;
public void adjustAge(int age) {
this.age = age + 20;
0
Kirill
9 November 2022, 23:19
I meant ("AGE' "+ PERSON.AGE); // equal 20,
but ("ADJUSTED AGE: "+ PERSON.AGE); equal 40
0
Денис Enterprise Java Developer
10 November 2022, 10:30
System.out.println method takes String as input and prints it to the console.
This particular line:
Or this one:
Means following:
String value "Adjusted age:" is concatenated with the actual value of the property person.age
The result should be "Adjusted age: 40"
Same for the first one - "Age: 20"
You may read about String concatenation to get a better understanding of this.
So basically what your code is doing is:
- Creating a new Person that has a predefined value in age property;
- Printing this value to the console;
- Calling to the person's object method adjustAge() that receives int value as argument and is setting a new value to the object's property age (in this particular case it is argument's value + 20). Since this method is called with an actual age value - you receive 20+20 in the end.
You can play around with it by replacing this:
With something like this:
After all these steps you are just printing new age value to the console. 0