Questions tagged [public]

`public` is an access-specifier in object-oriented languages; it indicates that all objects have access to the `public` field or method.

In object-oriented languages, classes specify how much access other classes can have to their members.

When a member has the public specifier, other classes have full access to that member.

Other access specifiers are protected and private.

You can use this tag for questions about how the public specifier controls (or fails to control) access to the members of a class, and questions on how it interacts with the other access specifiers.

1633 questions
52
votes
9 answers

C# public variable as writeable inside the class but readonly outside the class

I have a .Net C# class where I need to make a variable public. I need to initialize this variable within a method (not within the constructor). However, I don't want the variable to be modifieable by other classes. Is this possible?
MikeTWebb
  • 9,149
  • 25
  • 93
  • 132
50
votes
1 answer

Call-time pass-by-reference has been removed

Possible Duplicate: Call-time pass-by-reference has been deprecated While it may be documented somewhere on the internet, I cannot find a solution to my problem. Since the PHP 5.4 update, pass-by-references have been removed. Now I have a…
Sam Smith
  • 617
  • 1
  • 5
  • 6
48
votes
1 answer

Inline function cannot access non-public-API: @PublishedApi vs @Suppress vs @JvmSynthetic

In Kotlin, when I have a non-public member and an inline fun that calls it, there's a compilation error saying: Error:(22, 25) Kotlin: Public-API inline function cannot access non-public-API private fun f(): Unit defined in com.example I found…
hotkey
  • 140,743
  • 39
  • 371
  • 326
48
votes
3 answers

Public, Private - Upper Case, Lower Case:

New to GoLang, coming from Delphi, C++ : First time I tried to create my own package in Go, I followed all the instructions about how to lay out the workspace, etc, but I kept on getting a compiler error: ./myPackage.go:52: undefined: myFunc After…
Vector
  • 10,879
  • 12
  • 61
  • 101
43
votes
4 answers

How to write a simple class in C++?

I have been reading a lot of tutorials on C++ class but they miss something that other tutorials include. Can someone please show me how to write and use a very simple C++ class that uses visibility, methods and a simple constructor and destructor?
Babiker
  • 18,300
  • 28
  • 78
  • 125
43
votes
4 answers

C++ classes (public, private, and protected)

How can classes in C++ be declared public, private, or protected?
Simplicity
  • 47,404
  • 98
  • 256
  • 385
43
votes
14 answers

Should I use internal or public visibility by default?

I'm a pretty new C# and .NET developer. I recently created an MMC snapin using C# and was gratified by how easy it was to do, especially after hearing a lot of horror stories by some other developers in my organisation about how hard it is to do in…
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
40
votes
5 answers

Objective-C static, extern, public variables

I want to have a variable that I can access anywhere by importing a header file but I also want it to be static in the sense that there is only one of them created. In my .m file, I specify static BOOL LogStuff = NO; and in the initialize method I…
JPC
  • 8,096
  • 22
  • 77
  • 110
37
votes
3 answers

protected vs public constructor for abstract class? Is there a difference?

This question is out of curiosity. Is there a difference between: public abstract class MyClass { public MyClass() { } } and public abstract class MyClass { protected MyClass() { } } Thanks.
Marlon
  • 19,924
  • 12
  • 70
  • 101
37
votes
4 answers

How to publish ports in docker files?

I need to map the ports on the host to the ports on the container. I can achieve this by running the "docker run" command with the -p option. How do I achieve this through the Dockerfile? Using the following gives a "deprecated error" EXPOSE…
user_mda
  • 18,148
  • 27
  • 82
  • 145
33
votes
1 answer

Why @Rule annotated fields in JUnit has to be public?

In JUnit test case, a field annotated by @Rule must be public. It breaks a common Java coding convention (all class member variables should not be public). Why does JUnit require this? Documentation for @Rule:…
Shuhai Shen
  • 341
  • 3
  • 3
30
votes
8 answers

Why won't anyone accept public fields in C#?

Seems like every C# static analyzer wants to complain when it sees a public field. But why? Surely there are cases where a public (or internal) field is enough, and there is no point in having a property with its get_ and set_ methods? What if I…
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
30
votes
3 answers

Use of "Public" in a derived class declaration?

Given this base class: class Employee { char* name; int age; public: Employee(char* name); void print(); }; With regards to the "public", what's the difference between this: class Manager : public Employee { EmployeeList…
user189515
28
votes
5 answers

public or private, does it really matter with Android variables

inside of a single activity, when defining components to be used only within that activity, what's the real difference between the following definitions: Button btnPower = null; //or private Button btnPower = null; //or public Button btnPower =…
Octoth0rpe
  • 2,267
  • 4
  • 19
  • 21
26
votes
4 answers

C# Foreach statement does not contain public definition for GetEnumerator

I'm having a problem with a Windows Form application I'm building in C#. The error is stating "foreach statement cannot operate on variables of type 'CarBootSale.CarBootSaleList' because 'CarBootSale.CarBootSaleList' does not contain a public…
Danny
  • 601
  • 2
  • 9
  • 11