access-modifiers

Can I define “method-private” fields in Scala?

家住魔仙堡 提交于 2019-12-06 03:59:38
Given this situation: object ResourceManager { private var inited = false def init(config: Config) { if (inited) throw new IllegalStateException // do initialization inited = true } } Is there any way that I could make inited somehow “private to init()”, such that I can be sure that no other method in this class will ever be able to set inited = false ? Debilski Taken from In Scala, how would you declare static data inside a function? . Don’t use a method but a function object: val init = { // or lazy val var inited = false (config: Config) => { if (inited) throw new IllegalStateException

Inheritance breaking encapsulation?

穿精又带淫゛_ 提交于 2019-12-06 03:15:54
问题 People say inheritance breaks encapsulation, which i agree with. They say delegation is better- although the modifiers in delegation can also be public/protected. So is the real reason why inheritance breaks encapsulation because of the "knock-on" effect of the public/protected modifiers from the super class being exposed to any new classes which extend the current subclass? 回答1: Yes. Since it gives the derived class access to members of the base class (depending on what language and which

C++ private modifier ignored on nested anonymous struct

孤人 提交于 2019-12-05 14:17:42
问题 The following sample code compiles just fine in Visual C++: class Test { private: struct { struct { int privateData; }; }; }; int main(int, char **) { Test test; test.privateData = 0; return 0; } But why? I'd expect a compiler error because the privateData member should be inaccessible to the function main, since it's supposed to be private like its container's container. I know that nameless structs are not part of official C++, but this design is asinine. By the way I've also tried to

Is there a tool in eclipse, a plugin for eclipse, or an external program that can automatically limit access modifiers?

别来无恙 提交于 2019-12-05 09:16:30
I used to be incredibly bad at limiting access of my variables/methods/classes , I tended to use public a hell of a lot when I shouldn't. I was just wondering if there was any tool - plugin, external or otherwise - that can search your source code, find what calls your variables/methods/classes and changes the visibility if it's too high. So for example, if I had a public variable and nothing outside that class called it, then the tool would reduce its access to private. Mainly I need this for some of my older projects that have to many public variables. It would take far to long for me to

Access Modifiers - what's the purpose?

旧巷老猫 提交于 2019-12-05 09:13:54
I'm relatively new to programming in general and I was wondering if anyone could help me understand the purpose of access modifiers? I understand the fact that they set different levels of access for classes and variables etc. but why would you want to limit what has access to these? What is the point in not allowing access for different things? why not just allow access for everything? Sorry if this is a stupid question! There are thousands of reasons, but I'll quote a few from here and then expand on those: Hiding the internals of the object protects its integrity by preventing users from

Accessibility scope of Java access modifiers [duplicate]

扶醉桌前 提交于 2019-12-05 08:06:50
问题 This question already has answers here : What is the difference between public, protected, package-private and private in Java? (27 answers) Closed 6 years ago . Java has private, protected, and public access modifiers. Can you explain the accessibility scope of these modifiers. How can I access a protected member within a different package? 回答1: For better understanding you need to see this Access Modifiers Same Class Same Package Subclass Other packages public Y Y Y Y protected Y Y Y N no

Security impact of access modifiers (public, private, internal, protected)

廉价感情. 提交于 2019-12-05 05:26:37
Do the access modifiers of classes , properties or methods in C#, Java and other programming languages actually have an impact on the security of an application? Do they also protect against unauthorized access in some way? Or are they just a tool for clear and propper programming? No, access modifiers don't offer security protection. They are merely there for developer convenience, e.g. they help to enforce good coding practices and help with programming patterns. It's easy to access otherwise inaccessible modifiers by using reflection in Java/C# and other languages. The main purpose of the

The missing “framework level” access modifier

天大地大妈咪最大 提交于 2019-12-05 05:18:58
Here's the scenario. As a creator of publicly licensed, open source APIs, my group has created a Java-based web user interface framework (so what else is new?). To keep things nice and organized as one should in Java, we have used packages with naming convention org.mygroup.myframework.x, with the x being things like components, validators, converters, utilities, and so on (again, what else is new?). Now, somewhere in class org.mygroup.myframework.foo.Bar is a method void doStuff() that I need to perform logic specific to my framework, and I need to be able to call it from a few other places

Visual C# 2010 Express: Specify default access modifier for new classes?

寵の児 提交于 2019-12-05 01:34:01
Whenever I create new classes using Visual Studio 2010 Express C# it creates them with no access modifier. 9 times out of 10 I want my new classes to be public. How can I have Visual Studio create empty class templates with the "public" modifier by default? The trick is to create a new item template named Class. Then when you do Add > New Class, your template will be selected by default rather than the built-in Class template. (I am not sure if this behaviour is guaranteed but it Works On My Machine (TM).) To create the template: Right-click in your project and choose Add > Class. You can

Why does a Java constructor have to be public or protected for a class to be extended outside its package?

自古美人都是妖i 提交于 2019-12-04 23:09:10
问题 The following is my ProtectedConstructor.java source code: package protectCon; public class ProtectedConstructor{ public int nothing; ProtectedConstructor(){ nothing = 0; } } And following is the UsingProtectedCon.java source: package other; import protectcon.ProtectedConstructor; public class UsingProtectedCon extends ProtectedConstructor{ //**Line 4** public static void main(String... a) { } } When I compile UsingProtectedCon.java , I get error at Line 4 shown above. It says that