Which method is correct to access the value of count?
public class Person {
private String name;
private int age;
private static int count = 0;
}
A. private int getCount() {return (static)count;}
B. public static int getCount() {return count;}
C. public int getCount() {return static count;}
D. private static int getCount() {return count;}
How can you print the value of count?
public class Person {
private String name;
private int age;
private static int count=0;
public Person(String a, int b) {...}
public static int getCount() {return count;}
}
public class SomeOtherClass {
public static void main(String[] args) {
Person a = new Person("John Doe", 35);
}
}
A. System.out.println(a.getCount());
B. System.out.println(getCount());
C. N/A Compiler error
D. System.out.println(Person.getCount());
1. Which method is correct to access the value of count?
Correct answer is B) public static int getCount() {return count;}
Explaination: Public access modifier will allow to access value of count outside the class as well with return as count.
Return statement always as return keyword and value to be returned.
2. How can you print the value of count?
Correct answer is A) System.out.println(a.getCount());
Explaination: We have created object a of class Person inside class SomeOtherClass. getCount() method is of class Person so we can access it by object a of class Person a.getCount() in this way.
Get Answers For Free
Most questions answered within 1 hours.