Choose the appropriate access specifier for the attribute value so that it can be accessed from anywhere.class Test{ int value;}
Question
Choose the appropriate access specifier for the attribute value
so that it can be accessed from anywhere.
class Test {
int value;
}
Solution
In the given context, the attribute value
is already accessible from anywhere because it is declared as public
by default. In Java, when no access specifier is mentioned, it is considered as default
. But default
access specifier means it can be accessed only within the same package.
If you want to make sure that it can be accessed from anywhere, not just within the same package, you should use the public
access specifier. Here is how you can do it:
class Test{
public int value;
}
In this code, value
is an attribute of the class Test
and it is accessible from any other class in any package because we have specified it as public
.
Similar Questions
Which access specifier is usually used for data members of a class?a) Protectedb) Privatec) Publicd) Default
Select the correct answerWhich of the following access specifiers can be used for an interface?OptionsProtectedPublicPublic, protected, privatePrivate
Select the correct answerWhich of these access specifiers must be used for main() method?Optionsprivatepublicprotectednone of the mentioned
A/An can access all variables and methods of class.(a) datatype(c) fields(b) constructor(d) object
methods that should be used by any object that wants t change or retrieve the value of a data member.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.