access-modifiers

Can I Declare private variables inside a public method?

爷,独闯天下 提交于 2020-08-10 19:40:46
问题 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

Java define a explicit package-private modifier [closed]

眉间皱痕 提交于 2020-07-20 17:11:36
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Improve this question Obviously Java has a Access level package-private which achieved by not adding any explicit modifier. But isn't there a way to explicitly add this modifier? It's a bit confusing that we need to omit access level when we want to use member in package only

Java define a explicit package-private modifier [closed]

人盡茶涼 提交于 2020-07-20 17:08:17
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Improve this question Obviously Java has a Access level package-private which achieved by not adding any explicit modifier. But isn't there a way to explicitly add this modifier? It's a bit confusing that we need to omit access level when we want to use member in package only

multiple classes in a single file : modifier private not allowed here

∥☆過路亽.° 提交于 2020-07-06 06:23:34
问题 I am not able to understand why this code doesn't compile: class A { public static void main(String[] args) { System.out.println("hi"); } } private class B { int a; } I am saving the contents in a file named A.java - and I get an error: modifier private not allowed here // where I have defined class B This happens both when I try B as private and protected. Can someone please explain me the reason behind this? Thanks ! 回答1: From the Java Language specification: The access modifiers protected

multiple classes in a single file : modifier private not allowed here

爱⌒轻易说出口 提交于 2020-07-06 06:22:08
问题 I am not able to understand why this code doesn't compile: class A { public static void main(String[] args) { System.out.println("hi"); } } private class B { int a; } I am saving the contents in a file named A.java - and I get an error: modifier private not allowed here // where I have defined class B This happens both when I try B as private and protected. Can someone please explain me the reason behind this? Thanks ! 回答1: From the Java Language specification: The access modifiers protected

Can a package final variable be changed through reflection?

↘锁芯ラ 提交于 2020-06-13 08:16:57
问题 Can a package final variable be changed through reflection? Say I have this: public class Widget { final int val = 23; } Can the val be changed through reflection if made accessible? If so, is there any way to prevent it that without using the security manager? 回答1: Turns out, changing final members causes reflection-obtained values to differ from values returned by regular code! This is quite scary. import java.lang.reflect.Field; public class Test { private static final class Widget {

Can a package final variable be changed through reflection?

岁酱吖の 提交于 2020-06-13 08:16:32
问题 Can a package final variable be changed through reflection? Say I have this: public class Widget { final int val = 23; } Can the val be changed through reflection if made accessible? If so, is there any way to prevent it that without using the security manager? 回答1: Turns out, changing final members causes reflection-obtained values to differ from values returned by regular code! This is quite scary. import java.lang.reflect.Field; public class Test { private static final class Widget {