# Method visibility VS accessibility

**URL:** <https://www.ruby-forum.com/t/method-visibility-vs-accessibility/84465>\
**Category:** Ruby\
**Created:** [February 4, 2007, 10:49pm UTC](https://www.ruby-forum.com/t/method-visibility-vs-accessibility/84465 "2007-02-04T22:49:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 4, 2007, 10:49pm UTC](https://www.ruby-forum.com/t/method-visibility-vs-accessibility/84465/1 "2007-02-04T22:49:28Z")

</div>

Hi guys,  
I’ve done java for a few years and ruby is pretty new to me. In java, a  
method has visibility(private, protected, public) and  
accessibility(static, non-static); however, in ruby how can I declare  
the method: “public static method1”? It seem that the default visibility  
of a method is public in ruby, am i correct?  
One morething is that java program starts from “public static main()”.  
What is the entry point to ruby program?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 4, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/method-visibility-vs-accessibility/84465/2 "2007-02-04T23:25:27Z")

</div>

On 04.02.2007 22:49, Thai Le wrote:

> I’ve done java for a few years and ruby is pretty new to me. In java, a  
> method has visibility(private, protected, public) and

Actually there are four of them: you have to add “package” visibility to  
the list. 🙂

> accessibility(static, non-static); however, in ruby how can I declare  
> the method: “public static method1”? It seem that the default visibility  
> of a method is public in ruby, am i correct?

Yes. Strictly speaking there are no static methods in Ruby. But it has  
the concept of singleton methods that are defined for a single instance  
only. And since classes are just ordinary objects you can define  
instance methods of class objects and achieve basically the same as with  
Java’s static methods:

class Foo  
def self.a\_class\_method  
# …  
end

def an\_instance\_method  
self.class.a\_class\_method  
self.another\_class\_method  
end  
end

def Foo.another\_class\_method

# …

end

> One morething is that java program starts from “public static main()”.  
> What is the entry point to ruby program?

The situation is a bit different because Ruby is interpreted. If you  
want, you can view the root script you invoke as the body of main, i.e.,  
code is executed top down. However, part of this execution are class,  
method and constant definitions. Invocation arguments are accessible  
via ARGV (and also ARGF which is a special shortcut which will read from  
all files in ARGV). So, strictly speaking there is no equivalent of  
Java’s and C’s “main” but the functionality is there.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 5, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/method-visibility-vs-accessibility/84465/3 "2007-02-05T12:06:19Z")

</div>

Robert K.:

> class Foo  
> def self.a\_class\_method  
> # …  
> end
> 
> def an\_instance\_method  
> self.class.a\_class\_method  
> self.another\_class\_method  
> ^^^^^^^^^^^^^^^^^^^^^^^^^  
> end  
> end
> 
> def Foo.another\_class\_method
> 
> # …
> 
> end

It seems that you did not write what you meant in the marked line.

Kalman

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 6, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/method-visibility-vs-accessibility/84465/4 "2007-02-06T20:22:20Z")

</div>

Robert K. wrote:

> Yes. Strictly speaking there are no static methods in Ruby. But it has  
> the concept of singleton methods that are defined for a single instance  
> only. And since classes are just ordinary objects you can define  
> instance methods of class objects and achieve basically the same as with  
> Java’s static methods:
> 
> class Foo  
> def self.a\_class\_method  
> # …  
> end
> 
> def an\_instance\_method  
> self.class.a\_class\_method  
> self.another\_class\_method  
> end  
> end

So if i have a class method, I can invoke that method from anywhere by  
using class name like: Foo.a\_class\_method() without declaring public for  
that method?

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 7, 2007, 6:55am UTC](https://www.ruby-forum.com/t/method-visibility-vs-accessibility/84465/5 "2007-02-07T06:55:53Z")

</div>

On 6 feb, 16:22, Thai Le [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So if i have a class method, I can invoke that method from anywhere by  
> using class name like: Foo.a\_class\_method() without declaring public for  
> that method?

Yes. You can use either of:

Class::method  
or  
Class.method

---

<div class="post-metadata">

**Author:** ![lnthai2002](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a9adbd/32.png) [@lnthai2002](https://www.ruby-forum.com/u/lnthai2002)\
**Post date:** [February 5, 2007, 1:51pm UTC](https://www.ruby-forum.com/t/method-visibility-vs-accessibility/84465/6 "2007-02-05T13:51:26Z")

</div>

On 05.02.2007 11:58, Kalman N. wrote:

> > end  
> > end
> > 
> > def Foo.another\_class\_method
> > 
> > # …
> > 
> > end
> 
> It seems that you did not write what you meant in the marked line.

Correct. There is a “class.” missing.

robert
