The other answers have been accurate, but here's my take.
public class Item {
private Long id;
public static Item fromArchived(final ArchivedItem archived) {
return new Item(){{
this.id = archived.getId();
}};
}
}
Here the static method generates an anonymous class that extends Item. Item$id will be hidden from this anonymous class.
The following snippet is equivalent to the previous, but as a named nested class that exposes a constructor method rather than a method that calls a constructor.
public class Item {
private Long id;
class FromArchived extends Item {
public FromArchived(final ArchivedItem archived) {
this.id = archived.getId();
}
}
}
The visibility modifiers are enforced consistently whether the access is from a named, nested or anonymous class.
Finally, this following snippet should work as while inside a class, you have full access to all the private fields & methods of that class.
public class Item {
private Long id;
public static Item fromArchived(final ArchivedItem archived) {
Item item = new Item();
item.id = archived.getId();
return item;
}
}
This is true whether you're in a constructor, method or static method.