0

Guy you see a code, something like this-

Person person = new Person(); System.out.println("Age: " + person.age); person.adjustAge(person.age); System.out.println("Adjusted age: " + person.age); What can you conclude?. Me- I can conclude that- 1. There is a class named Person. 2.There is an instance variable named age. 3.A method named adjustAge. Is there anything more i can conclude/draw from the above four lines. Do tell me please.

29th Jun 2020, 4:56 PM
stephen haokip
stephen haokip - avatar
5 odpowiedzi
+ 2
Hello stephen haokip A class Person: - a constructor without parameters - a variable age - a void method adjustAge which takes a parameter I guess age is an integer and I guess adjustAge() is more like a setter which just change the age. An instance can always call a static method or variable so we can not say if the method and variable is static or non-static. It is also not possible to say something about access modifier.
29th Jun 2020, 6:55 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Obviously, there is concatenation on the fourth line.
29th Jun 2020, 5:18 PM
Samuel Adepoju
Samuel Adepoju - avatar
+ 1
if there is static member used as instanced, compiler creates warning, so as it is bad practice, probably there are not used static members of Person in the snippet. also there are static variable System.out with object which has print method and it accepts String as parameter
29th Jun 2020, 7:36 PM
zemiak
0
zemiak Thanks. I completely ignored System.out.println(). Maybe I have seen this too much :D But I didn't know that you get a warning when you use static members as instanced. Normally I don't do that.
30th Jun 2020, 9:53 AM
Denise Roßberg
Denise Roßberg - avatar
0
Denise, for warnings use -Xlint compiler options (I have java 11) eg: javac -d .class -Xlint:all Main.java class Person { static int age=30; } public class Main { public static void main(String[] args) { Person person = new Person(); System.out.println(person.age); } }
30th Jun 2020, 4:52 PM
zemiak