class-visibility

Is a class private or public by default in Java and C++?

孤街醉人 提交于 2020-01-09 19:41:19
问题 Are classes private or public by default in Java and C++? 回答1: Java : By default, the classes visibility is package private, i.e. only visible for classes in the same package. C++ : The class has no visibility defined like in Java. They are visible if you included them to the compilation unit. 回答2: In Java, a top-level class is either public or non-public. There is no "private". You can only use the public keyword or leave it off. If you leave it off it is non-public, i.e., visible only to

Access public static class' state from a separate class file

人走茶凉 提交于 2019-12-31 04:42:09
问题 I have a public static class within another public class as follows: public class Foo<A> { public static class Bar<A>{ A firstBar; Bar(A setBar){ this.firstBar=setBar; } } public final Bar<A> instanceBar; public Foo(A actualValue) { instanceBar = new Bar<A>(actualValue); } public Bar<A> getBar() { return instanceBar; } My objective is to access instanceBar 's state from a separate class file without a get method and without changing the visibility of firstBar . How do I accomplish this? For

Package and visibility

*爱你&永不变心* 提交于 2019-12-29 00:59:25
问题 I'm making an SDK and I'm trying to separate classes to different packages, those classes use some other shared classes. The issue is if I made the shared classes public everyone will be able to see them, not only my classes. What's the right way to make them only accessible by my application? Example : Package a MyClass1 Package b MyClass2 Package c public MySharedClass Because c is public MySharedClass will be able to access it, but the issue is that it will also will be visible to the

Disable use of AsyncTask, using custom AsyncTask

假装没事ソ 提交于 2019-12-23 04:34:48
问题 I have the following implementation of AsyncTask , allowing multiple AsyncTask s to run concurrently: public abstract class MyAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> { public AsyncTask<Params, Progress, Result> executeCompat(Params... params) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { return executeOnExecutor(THREAD_POOL_EXECUTOR, params); } else { return execute(params); } } } Now to avoid confusion and accidental use of the normal

Improvement of package-private classes in Java

ⅰ亾dé卋堺 提交于 2019-12-10 19:02:27
问题 In my experience, package-private visibility for classes in Java is turning out to be redundant. Package-private visibility seems to be based on the premise that a class which is almost-privately used by another class is likely to be kept in the same package. Often this is not the case. Is someone exploring an improved access modifier/alternate mechanism? Problem with trying to use package-private visibility: We are tempted to put functionally unrelated classes in same package to get this

C++ Access private member of nested class

拥有回忆 提交于 2019-12-10 17:54:28
问题 The title might be a bit misleading. I have the following problem: I have a tree consisting of leaves and internal nodes. The user should be able to store any information in the leaves and the tree has some methods which get a set of user-defined values and need to access the corresponding leaves in constant time (not amortized). I came up with the following idea but it does not work because unfortunately I cannot access private members of a nested class: The user creates the tree and also

PHP restrict calling of public methods [closed]

偶尔善良 提交于 2019-12-08 10:00:48
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I'm working on a library that has quite a few classes that are all pulled together by a central class. This central class has to call certain methods on the other classes for set-up/configuration purposes. These methods have to be public so that the central class can call them, but I do not want

When to package-private (no explicit modifier) in java?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 18:43:50
问题 I have been reading the tutorial Controlling Access to Members of a Class. I am confused what might be good use case for using package-private. Because as I understand, you can always change your package declaration to whatever the package declaration of such a class and act as if that is a public class. I understand that this is not a good thing to do, but what is stopping me? 回答1: Because as I understand, you can always change your package declaration to whatever the package declaration of

When to package-private (no explicit modifier) in java?

佐手、 提交于 2019-12-06 10:07:37
I have been reading the tutorial Controlling Access to Members of a Class . I am confused what might be good use case for using package-private. Because as I understand, you can always change your package declaration to whatever the package declaration of such a class and act as if that is a public class. I understand that this is not a good thing to do, but what is stopping me? aioobe Because as I understand, you can always change your package declaration to whatever the package declaration of such a class and act as if that is a public class Well, for one thing, the access modifiers are

Public Class - “is inaccessible due to its protection level. Only public types can be processed.”

心不动则不痛 提交于 2019-12-05 15:27:35
问题 I am doing a test project to learn about XML serialization of an object, and I am getting an odd runtime error: namespace SerializeTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void serializeConnection(Conn connection) { XmlSerializer serializer = new XmlSerializer(typeof(Conn)); TextWriter textWriter = new StreamWriter(@"serialized.xml"); serializer.Serialize(textWriter, connection);