# Java private variables in Ruby

**URL:** <https://www.ruby-forum.com/t/java-private-variables-in-ruby/134971>\
**Category:** Rails\
**Created:** [April 16, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/java-private-variables-in-ruby/134971 "2008-04-16T18:23:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 16, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/java-private-variables-in-ruby/134971/1 "2008-04-16T18:23:05Z")

</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:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 16, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/java-private-variables-in-ruby/134971/2 "2008-04-16T23:55:59Z")

</div>

On Apr 16, 5:22 pm, jlesleyball [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

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://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 16, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/java-private-variables-in-ruby/134971/3 "2008-04-16T23:57:09Z")

</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

On Apr 16, 1:20 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 17, 2008, 9:16am UTC](https://www.ruby-forum.com/t/java-private-variables-in-ruby/134971/4 "2008-04-17T09:16:55Z")

</div>

On Apr 16, 10:45 pm, jlesleyball [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?

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:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 17, 2008, 7:26am UTC](https://www.ruby-forum.com/t/java-private-variables-in-ruby/134971/5 "2008-04-17T07:26:19Z")

</div>

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

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

On Apr 16, 2008, at 1:20 PM, Frederick C.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 17, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/java-private-variables-in-ruby/134971/6 "2008-04-17T12:29:39Z")

</div>

Hi –

On Wed, 16 Apr 2008, Bcp wrote:

> 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

–  
Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 17, 2008, 9:33am UTC](https://www.ruby-forum.com/t/java-private-variables-in-ruby/134971/7 "2008-04-17T09: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  
@buf  
end

def buf=(value)  
@buf=value  
end
