# Java private variables in Ruby

**URL:** <https://discuss.rubyonrails.org/t/java-private-variables-in-ruby/28109>\
**Category:** rubyonrails-talk\
**Created:** [April 16, 2008, 4:22pm UTC](https://discuss.rubyonrails.org/t/java-private-variables-in-ruby/28109 "2008-04-16T16:22:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlesleyball](https://avatars.discourse-cdn.com/v4/letter/j/48db29/32.png) [@jlesleyball](https://discuss.rubyonrails.org/u/jlesleyball)\
**Post date:** [April 16, 2008, 4:22pm UTC](https://discuss.rubyonrails.org/t/java-private-variables-in-ruby/28109/1 "2008-04-16T16:22:19Z")

</div>

Hi

How would I achieve in Ruby what the following line does in Java?:

private BufferedImage buf;

I need a new BufferedImage, but I don't want to initialize it yet.

Thanks. Jennifer

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 16, 2008, 8:20pm UTC](https://discuss.rubyonrails.org/t/java-private-variables-in-ruby/28109/2 "2008-04-16T20:20:03Z")

</div>

You don't need to declare things in advance in ruby, and typing is done dynamically so the ruby equivalent of that statement is basically an empty line 🙂

Fred

---

<div class="post-metadata">

**Author:** ![jlesleyball](https://avatars.discourse-cdn.com/v4/letter/j/48db29/32.png) [@jlesleyball](https://discuss.rubyonrails.org/u/jlesleyball)\
**Post date:** [April 16, 2008, 9:45pm UTC](https://discuss.rubyonrails.org/t/java-private-variables-in-ruby/28109/3 "2008-04-16T21:45:44Z")

</div>

Hi Fred

Thanks for answering my question again. I wanted to make it a method variable because I wanted to initialize it within the scope of a conditional loop as well as use it later on in the action method. So ,that's why I thought I should declare it in the scope of the method. Is there something else I should be doing?

JB

---

<div class="post-metadata">

**Author:** ![Bala\_Paranj](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@Bala\_Paranj](https://discuss.rubyonrails.org/u/Bala_Paranj)\
**Post date:** [April 17, 2008, 5:25am UTC](https://discuss.rubyonrails.org/t/java-private-variables-in-ruby/28109/4 "2008-04-17T05:25:37Z")

</div>

Attributes of a class are private by default. You have to use attr  
acessor ang getter to get access to them.

[Http://www](Http://www).rubyplus.org Free Ruby & Rails screencasts

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 17, 2008, 7:16am UTC](https://discuss.rubyonrails.org/t/java-private-variables-in-ruby/28109/5 "2008-04-17T07:16:18Z")

</div>

You don't need to declare variables in advance in ruby like you do in java (1 exception: if you create a variable inside a block it won't be visible outside the block unless it already existed). If you really must buf = nil is all you need.

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [April 17, 2008, 7:33am UTC](https://discuss.rubyonrails.org/t/java-private-variables-in-ruby/28109/6 "2008-04-17T07:33:04Z")

</div>

Yea one of the great parts about ruby is how its dynamically typed. If you want to declare a private variable accessible throughout the class use an instance variable:

@buf = BufferedImage.new

or in a local scope (not accessible outside the class):

buf = ...

if you use an instance variable and want to access it outside the class (public interface):

attr\_accessor :buf

that automatically creates "getter" and "setter" for @buf which look like:

def buf &nbsp;&nbsp;@buf end

def buf=(value) &nbsp;&nbsp;&nbsp;@buf=value end

---

<div class="post-metadata">

**Author:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [April 17, 2008, 10:28am UTC](https://discuss.rubyonrails.org/t/java-private-variables-in-ruby/28109/7 "2008-04-17T10:28:57Z")

</div>

Hi --

> Attributes of a class are private by default. You have to use attr acessor ang getter to get access to them.

Or, to put it slightly differently: An object's instance variables are only visible to that object (i.e., only visible when 'self' is that object). If you expose an instance variable through getter and/or setter methods, then you've given your object an "attribute", though there's no real distinction between getter/setter methods that are implemented as simple wrappers around instance variables and those that aren't.

So instance variables are not, themselves, attributes; they're just instance variables, and they can (but do not have to) participate in the creation of attributes. And there's no language-level difference between an "attribute" and a getter and/or setter method in general (whether it uses instance variables or not).

David
