Correct answer is A, tested in online compiler.
static methods cannot access instance variable unless they are static. That is to say:
- either the "name" variable must be made static so that it can be accessed by the display() method on line 3
- or the display() method on line 3 must be made non-static (remove the static keyword) so that it can access the "name" variable
Only alternative that achieves one of these two is A.
Note that despite this change, i1.display("Flower") will still call the display() method on line 7. Non-static methods are allowed to access static instance variable regardless. In this case, since both "this.name" and "name" are null, "this.name += name;" will simply do "null += null;" and the result will be "nullnull".
The name field is an instance variable (non-static), but you are trying to access it directly from the static method display().So add static to the variable
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
supersquax
Highly Voted 5 months, 2 weeks agoSrinivasJasti
Most Recent 2 weeks, 2 days ago