Take the 2-minute tour ×

As the title says, in Ruby, 'keywords' such as private, public etc are actually "methods that operate on the class ,dynamically altering the visibility of the methods" ( http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ) - is this the same in Java?


share|improve this question
No. that is not the case in java. –  Chandra Sekhar Mar 29 '12 at 8:31

3 Answers 3

up vote 3 down vote accepted

No. In Java this maps to something in the bytecode the the JVM understands (and enforces). The compiler makes use of it itself, too.

There is no "dynamic compilation/class manipulation" that happens when the class is loaded, like you can do in Ruby or Perl.

share|improve this answer
Ok, so what is the difference between "something in the bytecode the JVM uses/understands to modify the method/variable/class" and "a method which modifies the method/variable/class"? Sorry if this sounds a little brash, but I can't see any real difference at the moment =/ –  Jarob22 Mar 29 '12 at 8:36
In Java you have a compiler that creates bytecode that the JVM executes. There is bytecode to control class and method visibility. At runtime, the class gets loaded from the bytecode as is. It is not modified from what the compiler created. Compare that to Ruby, where there is no real compile-step, and the whole class system gets dynamically created by executing some "meta" code. For example, in Ruby you can add a method to a class while the program is running (and that process is how all classes get created). In Java, you cannot do that. It's all the bytecode that was created beforehand. –  Thilo Mar 29 '12 at 9:00
Ahh I see! Thanks for the verbose and well-explained answer - very interesting :) –  Jarob22 Mar 29 '12 at 9:24

No, in Java they're "real" keywords: the modifiers end up in the generated bytecode. They're not methods.

share|improve this answer

No, they're not. Java is a compiled language, and those keywords are understood by the compiler with specific meanings when it parses your code.

Ruby isn't compiled up-front, so a class definition in Ruby is really an executable statement that defines the class at runtime. That's why access modifiers are actually runtime methods in Ruby. Class loading in Java is completely different.

share|improve this answer

Your Answer


