Questions tagged [public-method]

A public method is a method declaration that allows access from other classes external to the current class.

A public method is a method declaration that allows access from other classes external to the current class. Public methods are used in many Object-Oriented programming languages including Java, C#, and Objective-C

From http://www.javabeginner.com/learn-java/introduction-to-java-access-modifiers

Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

179 questions
305
votes
12 answers

Should methods in a Java interface be declared with or without a public access modifier?

Should methods in a Java interface be declared with or without the public access modifier? Technically it doesn't matter, of course. A class method that implements an interface is always public. But what is a better convention? Java itself is not…
Benno Richters
  • 14,488
  • 14
  • 38
  • 41
84
votes
7 answers

public vs. internal methods on an internal class

internal class Foo { public void Fee() { Debug.WriteLine("Fee"); } internal void Fi() { Debug.WriteLine("Fi"); } } I'm thinking that Fee() and Fi() are equally accessible since the entire class is already internal. Am I…
ScottS
  • 8,195
  • 3
  • 26
  • 48
81
votes
9 answers

Disable "not used" warning for public methods of a class

The new intellij upgrade (10.5) now shows a warning that some of the methods defined for a class are not being used. These methods are public and I plan on not using all of them as I have created them to support the API expected. I would like to…
45
votes
7 answers

"public static" or "static public"?

A minor point about function declaration keywords in PHP: If you've got a class method that's static, should the static keyword come before or after the visibility keyword (public, protected, private)? Assuming all your methods, static or…
dirtside
  • 7,666
  • 9
  • 40
  • 52
45
votes
8 answers

public methods in package-private classes

Does it make a difference to mark methods as public in package-private classes? class SomePackagePrivateClass { void foo(); // package private method public void bar(); // public method } Is there any practical difference in…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
41
votes
2 answers

How do I get the public methods of a class without inherited methods?

Given any object I can call #public_methods and see all the methods it will respond to. However, I find it would sometimes be handy to get a quick list of all the public methods that are not inherited, i.e. the stuff that's really part of this…
Andrew
  • 40,445
  • 46
  • 172
  • 276
40
votes
7 answers

What are the differences between "private", "public", and "protected methods"?

I'm learning Ruby, and have come up to a point where I am confused. The book I am using is talking about private, public, and protected methods, but I am still a bit confused. What are the differences between each?
Billjk
  • 9,249
  • 22
  • 49
  • 70
38
votes
5 answers

Why should we declare interface methods as public?

When I implement an interface method, I am forced to make it a public method. We may have cases where we want to use either the default (like in case of access within the same package) or protected. Can anyone please explain the reason behind this…
Vishnu
  • 434
  • 1
  • 7
  • 10
30
votes
6 answers

JQuery - Widget Public Methods

If I create a JQuery widget (code example below), and then define a "public" method, is there any other way to call the method other than using the following form? $("#list").list("publicMethod"); I would like to create a series of widgets that…
Steve
  • 45,586
  • 30
  • 89
  • 135
29
votes
7 answers

Private Methods Over Public Methods

I was examining the StringTokenizer.java class and there were a few questions that came to mind. I noticed that the public methods which are to be used by other classes invoked some private method which did all of the work. Now, I know that one of…
lbj-ub
  • 1,415
  • 2
  • 16
  • 33
27
votes
2 answers

On a nonconst object, why won't C++ call the const version of a method with public-const and private-nonconst overloads?

class C { public: void foo() const {} private: void foo() {} }; int main() { C c; c.foo(); } MSVC 2013 doesn't like this: > error C2248: 'C::foo' : cannot access private member declared in class 'C' If I cast to a const reference,…
27
votes
2 answers

Reference to a non-shared member requires an object reference occurs when calling public sub

I have a Public Class "General" in which is a Public Sub "updateDynamics". When I attempt to reference it in the code-behind for a page like so: updateDynamics(get_prospect.dynamicsID) I get the following error: reference to a non-shared member…
Dave Mackey
  • 4,078
  • 17
  • 70
  • 130
24
votes
1 answer

Why use public methods in JavaScript objects?

I'm part of a small study group at work that's trying to get a better grasp on what makes JavaScript tick. In our recent discussions about objects, we've learned that an object's public methods are recreated each time an object is instantiated,…
AurumPotabile
  • 331
  • 5
  • 16
24
votes
2 answers

Is there a way to declare public and private methods for S4 Reference Classes?

Up-front: I am aware that R is a functional language, so please don't bite ;-) I've had great experiences with using an OOP approach for a lot of my programs. Now, I'm wondering if there's a way to make a distinction between public and private…
Rappster
  • 11,680
  • 7
  • 58
  • 113
23
votes
11 answers

Why can't we use assertion for public methods?

Why can't we use assertion for public methods? I have read somewhere "An assert is inappropriate in public methods because the method guarantees that it will always enforce the argument checks. A public method must check its arguments…
developer
  • 9,003
  • 29
  • 80
  • 144
1
2 3
11 12