What is the output of this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the output of this code

Public static void main (string[ ] args) { Person p = new Person(); p. setAge(25); change(p); System.out.println(p.getAge()); } Static void change(Person p) { p. setAge(10); } I've tried it but still got an errors So plz help me to fix the errors

25th Jun 2019, 4:00 PM
Boy Arya
Boy Arya - avatar
12 Answers
+ 4
You need two classes: One for your main method and one called Person. public class Person{ private int age; //add a setter -> void setAge() //add a getter -> int getAge() } public class Program{ //All the code you have posted here } https://www.sololearn.com/learn/Java/2154/?ref=app
25th Jun 2019, 5:22 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
I think ans is 10
3rd May 2020, 9:46 AM
Tushar Prakash More
Tushar Prakash More - avatar
+ 3
10 is the answer
14th Sep 2021, 12:24 PM
Negar Arefi
Negar Arefi - avatar
+ 2
Where is the Person class defined?
25th Jun 2019, 4:07 PM
Anna
Anna - avatar
+ 2
Boy Arya You're still not defining the person class as you would need to allow for it to work in that way. The class would need to be defined outside of the class that the main function is in, and define all of the functions and methods for the person class within there instead. I would recommend going through the tutorial on classes and object oriented programming again as a refresher on how you're supposed to implement classes correctly https://www.sololearn.com/learn/Java/2155/
25th Jun 2019, 4:42 PM
Faisal
Faisal - avatar
+ 1
10
26th Feb 2023, 1:55 PM
kavindu shehan
0
10
29th Jul 2020, 10:05 AM
Lwin Lwin Han
Lwin Lwin Han - avatar
0
public class Program { public static void main(String[ ] args) { Person p = new Person(); p.setAge(25); change(p); System.out.println(p.getAge()); } static void change(Person p) { p.setAge(10); } } public class Person { private int age; public int getAge() { return age; } public void setAge(int a) { this.age = a; } }
18th Mar 2022, 9:54 PM
Nathan Morales
0
169 Comments Reference Types What is the output of this code? public static void main(String[ ] args) { Person p = new Person(); p.setAge(25); change(p); System.out.println(p.getAge()); } static void change(Person p) { p.setAge(10); } ans-10
30th Apr 2022, 8:22 AM
MAREESWARAN S
MAREESWARAN S - avatar
0
10
30th Jun 2022, 4:32 PM
Opanuga Stephen
Opanuga Stephen - avatar
- 1
10 is the answer
10th Jun 2021, 3:45 PM
PAKKI CHRISTIE VICTORIA TIRZAH
- 3
I've tried to change the code like u said but still errors public class Person { public static void main(String[] args) { Person p = new Person(); p.setAge(25); change(p); System.out.println (p.getAge()); } static void change(Person p) { p.setAge (10); } } Tell me what wrong with my code
25th Jun 2019, 4:18 PM
Boy Arya
Boy Arya - avatar