Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'private' mean in Groovy?

Tags:

The following code sets a private method. So how private really is private?

public class Person {  private String name }  def u = new Person(name:"Ron") println u.name 
like image 245
ripper234 Avatar asked Oct 23 '10 19:10

ripper234


People also ask

Does Groovy have private methods?

I used to code in Java before I met groovy. Like most of you, groovy attracted me with many enhancements. This was to my surprise to discover that method visibility in groovy is handled different than Java!

What are Groovy properties?

When a Groovy class definition declares a field without an access modifier, then a public setter/getter method pair and a private instance variable field is generated which is also known as "property" according to the JavaBeans specification.

What is this in Groovy?

" this " in a block mean in Groovy always (be it a normal Java-like block or a Closure) the surrounding class (instance). " owner " is a property of the Closure and points to the embedding object, which is either a class (instance), and then then same as " this ", or another Closure.

What is new keyword in Groovy?

2.1. This means they can be instantiated without restrictions from any other classes or scripts. This way, they can only be public (even though the public keyword may be suppressed). Classes are instantiated by calling their constructors, using the new keyword, as in the following snippet. def p = new Person()


1 Answers

By design Groovy should respect the private modifier, however the current implementation takes no account of it.

There are further details in groovy call private method in Java super class

like image 127
mfloryan Avatar answered Sep 19 '22 12:09

mfloryan