问题
This question was asked from me in an interview.
Can we declare private variables inside a public method?
If can, can we access variables through the public method?
I tried with java but it does not allow to define private variable inside a method why is that?
public class GetUser {
public String getUserName(){
private String user="David";
return user;
}}
回答1:
No, you can not. Variables inside method are considered local and can't have modifiers. You can have private fields in a class, but this is different:
public class Test {
public String getUserName(){
user="David";
return user;
}
private String user;
}
Besides, naming a class GetUser
is kind of awkward.
A better class name would be User
. getUser
is more appropriate for a method name.
回答2:
- You can not declare any variable with access specifier within a method.
- You can do it by defining inner class and declaring private variables inside that.
- And yes that can be accessed from that method.
EDIT: Read more about Inner Class here
回答3:
No, it is a syntax error, you can not use access modifiers within a method.
Access modifiers are used to determine the scope of some members (attributes, constructors, methods) of the class. The scope of a method variable is only that method, so you do not need to specify the scope explicitly.
More about access modifiers: https://www.javatpoint.com/access-modifiers
回答4:
A variable declared directly within a method is a local variable. A local variable cannot be declared as private
. In fact, the only modifier that you can use on a local variable declaration is final
.
It is also possible to embed an inner class declaration inside a method; either an anonymous inner class or a named inner class. The inner class can declare fields, and those fields can have the full range of modifiers apart from static
. In a sense, the fields are variables "inside" the enclosing method. Here is an example:
public void myMethod () {
class Inner() {
private int field1;
public int field2;
/* .... */
}
Inner inner = new Inner();
// do stuff with the inner object
}
If you play around with the above code, you will see that the code in myMethod
has full access to the private fields of Inner
. But any, this is an example of a private
variable being declared "inside" a method.
Returning to normal case, the purpose of access modifiers is to implement encapsulation. And the main purpose of encapsulation is to minimize undesirable coupling between different parts of the code base. (It is bad for class to be able to access the internal variables of another class because it makes it hard to change the second classes code ... without breaking the first class.)
But the only code that can access a method's local variables is close by; i.e. in the same source code file, and within the method's body. The assumption is that any code within the body may need access to the local variable, and any code that shouldn't have access (e.g. to avoid coupling) should be in a different method.
Thus, the "natural" visibility of a local variable is equivalent to private
, and other visibility settings make little sense. So, given that all local variables are "naturally" private, then explicitly declaring them as private
would just be syntactic noise. The language doesn't allow it.
来源:https://stackoverflow.com/questions/50653132/can-i-declare-private-variables-inside-a-public-method