private

Why it is recommended to declare instance variables as private?

倖福魔咒の 提交于 2020-01-12 08:05:12
问题 My question relates to Java, but it also can be applied to C#. I was wondering why everybody recommends making the instance variables private as opposed to making them protected . Let's just think about it. Private variables are not seen by the subclass, so if I need to access or change the variables of the superclass in my subclass I am forced to use some accessor and mutators method like getMyPrivateVariable or setMyPrivateVariable . But when you extend some class and inherit its members,

Performance differences between static and non-static final primitive fields in Java

跟風遠走 提交于 2020-01-11 05:04:08
问题 I recently ran across a class that had the following field declared: private final int period = 1000; In this particular case, the author had intended for it to also be static and since the value couldn't be altered at any point, there was no real functional reason not to declare it static, but it got me wondering how Java treats final vs. final static primitives. In particular: 1) How are final static primitives stored? Are they simply compiled directly into the expressions in which they're

Performance differences between static and non-static final primitive fields in Java

南笙酒味 提交于 2020-01-11 05:03:32
问题 I recently ran across a class that had the following field declared: private final int period = 1000; In this particular case, the author had intended for it to also be static and since the value couldn't be altered at any point, there was no real functional reason not to declare it static, but it got me wondering how Java treats final vs. final static primitives. In particular: 1) How are final static primitives stored? Are they simply compiled directly into the expressions in which they're

Youtube integration in iOS SDK

随声附和 提交于 2020-01-10 05:47:08
问题 How I can integrate Youtube with iOS? Mainly I want to play private videos without asking for login. Login should be hard coded in the app and will not ask user before playing the private video. 回答1: Some YouTube videos can be played back in a MPMoviePlayerController - some cannot. When you query a video through the YouTube Data API https://developers.google.com/youtube/2.0/reference#Videos_feed you'll get back all the content types available for a particular videos, including the Flash

Access non-public (java-native) classes from JDK (7)

可紊 提交于 2020-01-06 14:16:19
问题 I want to use the method MethodHandleNatives.getTargetMethod(MethodHandle)AccessibleObject. The class MethodHandleNatives is not public. So does anybody know how I can do that? I know that its possible to access private methods and fields via reflections, so I am asking, if this is also possible. Thanks. 回答1: I have found a solution. It is not straight forward but it works =) MethodHandle mh; // a MethodHandle Object Class<?> mhn; try { mhn = Class.forName("java.lang.invoke

Whether to use private modifier for all fields in a standalone swing applicaton?

强颜欢笑 提交于 2020-01-06 08:09:00
问题 I have a standalone Swing application with a main class which I will call MainClass. The only access to this MainClass is from the main(String args[]) method of this class. In other words, this class is not going to be used by any other class. Should I make the member fields of MainClass "private"? I argue that it is redundant since no other class is going to instantiate MainClass, but another more experienced programmer argues that it violated Java best practices to not label a field

Twig accessing protected/private model variables

孤者浪人 提交于 2020-01-05 08:03:21
问题 I have a problem with Twig, (in fact this is not really a problem but it's disturbing to me) I have a Post model class in php and i have some protected variables (I also tried with private ). To acces them I have a public function in php getMyVariable . If in my controller I try to echo the protected variable it throz me an error Cannot access protected property... so I have to use the function to echo my variable. This is totally normal and this is what I want But, then I try to render it in

Is there a need to declare access modifiers in a private inner class

泪湿孤枕 提交于 2020-01-04 01:55:28
问题 Let's say I have a class like this: public class OuterClass { //... private class InnerClass { private int x; // This variable makes sense public int y; // Is there any use for this? } } In the code above, since the inner class is private, only the outer class has access to all its variables, even the private ones. The inner class itself is not visible to any other class but the enclosing outer class. So even though variable y above is public, it cannot be accessed by any other class other

Creating multiple read-only properties dynamically in AS3

纵然是瞬间 提交于 2020-01-03 15:31:53
问题 I have a class which at the moment is quite messy/repetitive: public class AvFramework extends Object { // vars private var _handler:AvHandler; private var _keyboard:AvKeyboard; private var _manager:AvManager; /** * Constructor */ public function AvFramework() { _handler = new AvHandler(); _keyboard = new AvKeyboard(); _manager = new AvManager(); // attach _handler.framework = this; _keyboard.framework = this; _manager.framework = this; } /** * Getters */ public function get keyboard()

Automatically change public to private (Java)

非 Y 不嫁゛ 提交于 2020-01-02 04:12:09
问题 I am doing a refactoring on code is translated from other languages into Java and I want to do it automatically. My problem is that I have a lot of methods that aren't private but are just called in the same class that they are declared and I want to make them private. I have a lot of classes and I guess if there is something that can help me to do it semi-automatically I would like to know it. Do you know if I can look for these methods to make them private fastly? I am using Eclipse. 回答1: