Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:24:22+00:00 2026-05-25T10:24:22+00:00

Combining private field access with the CRTP in Java seems to tickle a weird

  • 0

Combining private field access with the CRTP in Java seems to tickle a weird edge case in the visibility rules:

public abstract class Test<O extends Test<O>> implements Cloneable {
    private int x = 0;

    @SuppressWarnings("unchecked")
    @Override
    protected final O clone() {
        try {
            return (O) super.clone();
        } catch (CloneNotSupportedException ex) {
            throw new AssertionError(ex);
        }
    }

    public final int getX() {
        return x;
    }

    public final O withX(int x) {
        O created = clone();
        created.x = x;  // Compiler error: The field Test<O>.x is not visible
        return created;
    }
}

Simply changing the withX() method to this…

    public final O withX(int x) {
        O created = clone();
        Test<O> temp = created;
        temp.x = x;
        return created;
    }

…makes the code compile. I tested this in Oracle’s javac and in Eclipse’s compiler. What gives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:24:23+00:00Added an answer on May 25, 2026 at 10:24 am

    This isnt actually an issue with generics. The JLS inheritance rules keep private fields from being visible in sub classes. Because X is private, it is not a member of type O, even though it is a member of type Test<O> and O is a subtype of Test<O>. If you had used code like:

    public final O withX(int x) {
        Test<O> created = clone();
        created.x = x;
        return (O) created;
    }
    

    It would work. This is an instance where LSP is not upheld by Java, but it is only a local problem with the type system, since private fields are only available to objects of the same type. If it didn’t work this way than private fields wouldn’t really be private. I dont think having a special exception to the rules for recursive templates is a good idea.

    Note, this is never actually a limitation of what you can do. You can always cast the subtype up to the the supertype when you want to make the change, just like you do in your alternative code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Specifically, in relation to field initializers (in this case, static) - §17.11 in ECMA
Method 'Private Sub trigger_Triggered(sender As Object, e As OamsIDD.VoamsWorldIDD.OamsStateChangeEventArgs)' cannot handle event 'Public Event
First of all i created a class called PrivateShirt1 and called a private field
Consider the following code: public abstract class RepositoryBase<T> where T : class { #region
Mono is throwing the warning: warning CS0414: The private field 'Convergence.Core.Services.PermissionService.userService' is assigned but
Suppose I have the following hierarchy: class A { public: A() private: int aa;
I recently came across a piece of code: public class SomeClass { private Logger
We have a class hierarchy similar to this one: public class TestDereference { private
Does combining an Enterprise Messaging solution with Web Services result in a real performance
Any tips on combining multiple .js files into 1 (for a build process). Will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.