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
12 Réponses
+ 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
+ 3
I think ans is
10
+ 3
10 is the answer
+ 2
Where is the Person class defined?
+ 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/
+ 1
10
0
10
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;
}
}
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
0
10
- 1
10 is the answer
- 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